Skip to content

Commit

Permalink
Migrated logger code to f-style
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdelprete committed Jan 17, 2025
1 parent 686ad6b commit 2a718ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion custom_components/abb_powerone_pvi_sunspec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async def async_unload_entry(
hass.data[DOMAIN].pop(config_entry.entry_id)
_LOGGER.debug("Removed config entry from hass data")
except Exception as ex:
_LOGGER.error("Error during unload: %s", str(ex))
_LOGGER.error(f"Error during unload: {str(ex)}")
return False
else:
_LOGGER.debug("Failed to unload platforms")
Expand Down
30 changes: 21 additions & 9 deletions custom_components/abb_powerone_pvi_sunspec/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import socket
import threading

from homeassistant.core import HomeAssistant
from pymodbus.client import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.exceptions import ConnectionException, ModbusException
Expand Down Expand Up @@ -41,15 +42,26 @@ class ABBPowerOneFimerAPI:

def __init__(
self,
hass,
name,
host,
port,
slave_id,
base_addr,
scan_interval,
):
"""Initialize the Modbus API Client."""
hass: HomeAssistant,
name: str,
host: str,
port: int,
slave_id: int,
base_addr: int,
scan_interval: int,
) -> None:
"""Initialize the Modbus API Client.
Args:
hass: HomeAssistant instance
name: Device name
host: Device IP address
port: Modbus TCP port
slave_id: Modbus slave ID (1-247)
base_addr: Base address for register reads
scan_interval: Update interval in seconds
"""
self._hass = hass
self._name = name
self._host = host
Expand Down
31 changes: 16 additions & 15 deletions custom_components/abb_powerone_pvi_sunspec/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ class ABBPowerOneFimerCoordinator(DataUpdateCoordinator):

def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Initialize data update coordinator."""

# get scan_interval from user config
self.scan_interval = config_entry.data.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
# get parameters from user config
self.conf_name = config_entry.data.get(CONF_NAME)
self.conf_host = config_entry.data.get(CONF_HOST)
self.conf_port = int(config_entry.data.get(CONF_PORT))
self.conf_slave_id = int(config_entry.data.get(CONF_SLAVE_ID))
self.conf_base_addr = int(config_entry.data.get(CONF_BASE_ADDR))
self.scan_interval = int(
config_entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
)

# enforce scan_interval lower bound
if self.scan_interval < MIN_SCAN_INTERVAL:
self.scan_interval = MIN_SCAN_INTERVAL
Expand All @@ -61,21 +66,17 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:

self.api = ABBPowerOneFimerAPI(
hass,
config_entry.data.get(CONF_NAME),
config_entry.data.get(CONF_HOST),
config_entry.data.get(CONF_PORT),
config_entry.data.get(CONF_SLAVE_ID),
config_entry.data.get(CONF_BASE_ADDR),
self.conf_name,
self.conf_host,
self.conf_port,
self.conf_slave_id,
self.conf_base_addr,
self.scan_interval,
)

_LOGGER.debug("Coordinator Config Data: %s", config_entry.data)
_LOGGER.debug(f"Coordinator Config Data: {config_entry.data}")
_LOGGER.debug(
"Coordinator API init: Host: %s Port: %s ID: %s ScanInterval: %s",
config_entry.data.get(CONF_HOST),
config_entry.data.get(CONF_PORT),
config_entry.data.get(CONF_SLAVE_ID),
self.scan_interval,
f"Coordinator init - Host: {self.conf_host} Port: {self.conf_port} ID: {self.conf_slave_id} Base Addr.: {self.conf_base_addr} ScanInterval: {self.scan_interval}"
)

async def async_update_data(self):
Expand Down
19 changes: 8 additions & 11 deletions custom_components/abb_powerone_pvi_sunspec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ async def async_setup_entry(
# Get handler to coordinator from config
coordinator: ABBPowerOneFimerCoordinator = config_entry.runtime_data.coordinator

_LOGGER.debug("(sensor) Name: %s", config_entry.data.get(CONF_NAME))
_LOGGER.debug("(sensor) Manufacturer: %s", coordinator.api.data["comm_manufact"])
_LOGGER.debug("(sensor) Model: %s", coordinator.api.data["comm_model"])
_LOGGER.debug("(sensor) SW Version: %s", coordinator.api.data["comm_version"])
_LOGGER.debug("(sensor) Inverter Type (str): %s", coordinator.api.data["invtype"])
_LOGGER.debug("(sensor) MPPT #: %s", coordinator.api.data["mppt_nr"])
_LOGGER.debug("(sensor) Serial#: %s", coordinator.api.data["comm_sernum"])
_LOGGER.debug(f"(sensor) Name: {config_entry.data.get(CONF_NAME)}")
_LOGGER.debug(f"(sensor) Manufacturer: {coordinator.api.data['comm_manufact']}")
_LOGGER.debug(f"(sensor) Model: {coordinator.api.data['comm_model']}")
_LOGGER.debug(f"(sensor) SW Version: {coordinator.api.data['comm_version']}")
_LOGGER.debug(f"(sensor) Inverter Type (str): {coordinator.api.data['invtype']}")
_LOGGER.debug(f"(sensor) MPPT #: {coordinator.api.data['mppt_nr']}")
_LOGGER.debug(f"(sensor) Serial#: {coordinator.api.data['comm_sernum']}")

sensor_list = []
add_sensor_defs(coordinator, config_entry, sensor_list, SENSOR_TYPES_COMMON)
Expand All @@ -76,10 +76,7 @@ async def async_setup_entry(
)

_LOGGER.debug(
"(sensor) DC Voltages : single=%s dc1=%s dc2=%s",
coordinator.api.data["dcvolt"],
coordinator.api.data["dc1volt"],
coordinator.api.data["dc2volt"],
f"(sensor) DC Voltages : single={coordinator.api.data['dcvolt']} dc1={coordinator.api.data['dc1volt']} dc2={coordinator.api.data['dc2volt']}"
)
if coordinator.api.data["mppt_nr"] == 1:
add_sensor_defs(
Expand Down

0 comments on commit 2a718ca

Please sign in to comment.