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 88036f7 commit 07623a6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
4 changes: 2 additions & 2 deletions custom_components/sinapsi_alfa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: SinapsiAlfaConfig

if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})
_LOGGER.info(STARTUP_MESSAGE)
_LOGGER.info(f"{STARTUP_MESSAGE}")
_LOGGER.debug(f"Setup config_entry for {DOMAIN}")

# Initialise the coordinator that manages data updates from your api.
Expand Down Expand Up @@ -152,7 +152,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
26 changes: 17 additions & 9 deletions custom_components/sinapsi_alfa/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from getmac import getmac
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.core import HomeAssistant
from pymodbus.client import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.exceptions import ConnectionException, ModbusException
Expand All @@ -33,13 +34,22 @@ class SinapsiAlfaAPI:

def __init__(
self,
hass,
name,
host,
port,
scan_interval,
hass: HomeAssistant,
name: str,
host: str,
port: int,
scan_interval: int,
):
"""Initialize the Modbus API Client."""
"""Initialize the Modbus API Client.
Args:
hass: HomeAssistant instance
name: Device name
host: Device IP address
port: Modbus TCP port
scan_interval: Update interval in seconds
"""
self._hass = hass
self._name = name
self._host = host
Expand Down Expand Up @@ -211,9 +221,7 @@ async def async_get_data(self):
try:
if self.connect():
_LOGGER.debug(
"Start Get data (Host: %s - Port: %s)",
self._host,
self._port,
f"Start Get data (Host: {self._host} - Port: {self._port})",
)
# HA way to call a sync function from async function
# https://developers.home-assistant.io/docs/asyncio_working_with_async?#calling-sync-functions-from-async
Expand Down
23 changes: 12 additions & 11 deletions custom_components/sinapsi_alfa/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ class SinapsiAlfaCoordinator(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.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 @@ -59,18 +63,15 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:

self.api = SinapsiAlfaAPI(
hass,
config_entry.data.get(CONF_NAME),
config_entry.data.get(CONF_HOST),
config_entry.data.get(CONF_PORT),
self.conf_name,
self.conf_host,
self.conf_port,
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),
self.scan_interval,
f"Coordinator init - Host: {self.conf_host} Port: {self.conf_port} ScanInterval: {self.scan_interval}"
)

async def async_update_data(self):
Expand Down
8 changes: 4 additions & 4 deletions custom_components/sinapsi_alfa/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ async def async_setup_entry(
# This gets the data update coordinator from hass.data as specified in your __init__.py
coordinator: SinapsiAlfaCoordinator = config_entry.runtime_data.coordinator

_LOGGER.debug("(sensor) Name: %s", config_entry.data.get(CONF_NAME))
_LOGGER.debug("(sensor) Manufacturer: %s", coordinator.api.data["manufact"])
_LOGGER.debug("(sensor) Model: %s", coordinator.api.data["model"])
_LOGGER.debug("(sensor) Serial#: %s", coordinator.api.data["sn"])
_LOGGER.debug(f"(sensor) Name: {config_entry.data.get(CONF_NAME)}")
_LOGGER.debug(f"(sensor) Manufacturer: {coordinator.api.data['manufact']}")
_LOGGER.debug(f"(sensor) Model: {coordinator.api.data['model']}")
_LOGGER.debug(f"(sensor) Serial#: {coordinator.api.data['sn']}")

sensors = []
for sensor in SENSOR_ENTITIES:
Expand Down

0 comments on commit 07623a6

Please sign in to comment.