From 3460e0b074881ae62fe3019538325f71eb3c7a88 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 15 Oct 2022 15:38:47 +0200 Subject: [PATCH] Add type hints to aqualogic (#80328) --- .strict-typing | 1 + .../components/aqualogic/__init__.py | 23 +++++++++++-------- homeassistant/components/aqualogic/sensor.py | 12 ++++++---- homeassistant/components/aqualogic/switch.py | 11 ++++----- mypy.ini | 10 ++++++++ 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/.strict-typing b/.strict-typing index 623653ac203dc2..2e6d68edc4c448 100644 --- a/.strict-typing +++ b/.strict-typing @@ -57,6 +57,7 @@ homeassistant.components.ambient_station.* homeassistant.components.amcrest.* homeassistant.components.ampio.* homeassistant.components.anthemav.* +homeassistant.components.aqualogic.* homeassistant.components.aseko_pool_live.* homeassistant.components.asuswrt.* homeassistant.components.auth.* diff --git a/homeassistant/components/aqualogic/__init__.py b/homeassistant/components/aqualogic/__init__.py index 94941b30713324..28e57c2b351e12 100644 --- a/homeassistant/components/aqualogic/__init__.py +++ b/homeassistant/components/aqualogic/__init__.py @@ -1,4 +1,6 @@ """Support for AquaLogic devices.""" +from __future__ import annotations + from datetime import timedelta import logging import threading @@ -13,7 +15,7 @@ EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import Event, HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.typing import ConfigType @@ -50,7 +52,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: class AquaLogicProcessor(threading.Thread): """AquaLogic event processor thread.""" - def __init__(self, hass, host, port): + def __init__(self, hass: HomeAssistant, host: str, port: int) -> None: """Initialize the data object.""" super().__init__(daemon=True) self._hass = hass @@ -59,27 +61,28 @@ def __init__(self, hass, host, port): self._shutdown = False self._panel = None - def start_listen(self, event): + def start_listen(self, event: Event) -> None: """Start event-processing thread.""" _LOGGER.debug("Event processing thread started") self.start() - def shutdown(self, event): + def shutdown(self, event: Event) -> None: """Signal shutdown of processing event.""" _LOGGER.debug("Event processing signaled exit") self._shutdown = True - def data_changed(self, panel): + def data_changed(self, panel: AquaLogic) -> None: """Aqualogic data changed callback.""" dispatcher_send(self._hass, UPDATE_TOPIC) - def run(self): + def run(self) -> None: """Event thread.""" while True: - self._panel = AquaLogic() - self._panel.connect(self._host, self._port) - self._panel.process(self.data_changed) + panel = AquaLogic() + self._panel = panel + panel.connect(self._host, self._port) + panel.process(self.data_changed) if self._shutdown: return @@ -88,6 +91,6 @@ def run(self): time.sleep(RECONNECT_INTERVAL.total_seconds()) @property - def panel(self): + def panel(self) -> AquaLogic | None: """Retrieve the AquaLogic object.""" return self._panel diff --git a/homeassistant/components/aqualogic/sensor.py b/homeassistant/components/aqualogic/sensor.py index d575beb03677cb..e8abc3bae6235c 100644 --- a/homeassistant/components/aqualogic/sensor.py +++ b/homeassistant/components/aqualogic/sensor.py @@ -24,7 +24,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import DOMAIN, UPDATE_TOPIC +from . import DOMAIN, UPDATE_TOPIC, AquaLogicProcessor @dataclass @@ -120,7 +120,7 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the sensor platform.""" - processor = hass.data[DOMAIN] + processor: AquaLogicProcessor = hass.data[DOMAIN] monitored_conditions = config[CONF_MONITORED_CONDITIONS] entities = [ @@ -138,7 +138,11 @@ class AquaLogicSensor(SensorEntity): entity_description: AquaLogicSensorEntityDescription _attr_should_poll = False - def __init__(self, processor, description: AquaLogicSensorEntityDescription): + def __init__( + self, + processor: AquaLogicProcessor, + description: AquaLogicSensorEntityDescription, + ) -> None: """Initialize sensor.""" self.entity_description = description self._processor = processor @@ -153,7 +157,7 @@ async def async_added_to_hass(self) -> None: ) @callback - def async_update_callback(self): + def async_update_callback(self) -> None: """Update callback.""" if (panel := self._processor.panel) is not None: if panel.is_metric: diff --git a/homeassistant/components/aqualogic/switch.py b/homeassistant/components/aqualogic/switch.py index e04bc8595fae0e..e693df0a0c1502 100644 --- a/homeassistant/components/aqualogic/switch.py +++ b/homeassistant/components/aqualogic/switch.py @@ -14,7 +14,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import DOMAIN, UPDATE_TOPIC +from . import DOMAIN, UPDATE_TOPIC, AquaLogicProcessor SWITCH_TYPES = { "lights": "Lights", @@ -47,7 +47,7 @@ async def async_setup_platform( """Set up the switch platform.""" switches = [] - processor = hass.data[DOMAIN] + processor: AquaLogicProcessor = hass.data[DOMAIN] for switch_type in config[CONF_MONITORED_CONDITIONS]: switches.append(AquaLogicSwitch(processor, switch_type)) @@ -59,7 +59,7 @@ class AquaLogicSwitch(SwitchEntity): _attr_should_poll = False - def __init__(self, processor, switch_type): + def __init__(self, processor: AquaLogicProcessor, switch_type: str) -> None: """Initialize switch.""" self._processor = processor self._state_name = { @@ -77,12 +77,11 @@ def __init__(self, processor, switch_type): self._attr_name = f"AquaLogic {SWITCH_TYPES[switch_type]}" @property - def is_on(self): + def is_on(self) -> bool: """Return true if device is on.""" if (panel := self._processor.panel) is None: return False - state = panel.get_state(self._state_name) - return state + return panel.get_state(self._state_name) # type: ignore[no-any-return] def turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" diff --git a/mypy.ini b/mypy.ini index 7e5133f38e95a6..6f6e1921bb5b0d 100644 --- a/mypy.ini +++ b/mypy.ini @@ -322,6 +322,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.aqualogic.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.aseko_pool_live.*] check_untyped_defs = true disallow_incomplete_defs = true