From 08cebb247f17365e86d586581e3734689a465647 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Mon, 4 Oct 2021 22:13:11 +0200 Subject: [PATCH] Activate mypy for rpi_power (#57047) --- .strict-typing | 1 + .../components/rpi_power/__init__.py | 2 +- .../components/rpi_power/binary_sensor.py | 53 ++++++------------- .../components/rpi_power/config_flow.py | 2 +- mypy.ini | 14 +++-- script/hassfest/mypy_config.py | 1 - 6 files changed, 31 insertions(+), 42 deletions(-) diff --git a/.strict-typing b/.strict-typing index 18dc1be41eb..dec244ece15 100644 --- a/.strict-typing +++ b/.strict-typing @@ -91,6 +91,7 @@ homeassistant.components.recorder.statistics homeassistant.components.remote.* homeassistant.components.renault.* homeassistant.components.rituals_perfume_genie.* +homeassistant.components.rpi_power.* homeassistant.components.samsungtv.* homeassistant.components.scene.* homeassistant.components.select.* diff --git a/homeassistant/components/rpi_power/__init__.py b/homeassistant/components/rpi_power/__init__.py index eeb7c4fe181..320043a0260 100644 --- a/homeassistant/components/rpi_power/__init__.py +++ b/homeassistant/components/rpi_power/__init__.py @@ -11,6 +11,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/rpi_power/binary_sensor.py b/homeassistant/components/rpi_power/binary_sensor.py index 79ef36e891a..f223bbf2c91 100644 --- a/homeassistant/components/rpi_power/binary_sensor.py +++ b/homeassistant/components/rpi_power/binary_sensor.py @@ -5,7 +5,7 @@ Minimal Kernel needed is 4.14+ """ import logging -from rpi_bad_power import new_under_voltage +from rpi_bad_power import UnderVoltage, new_under_voltage from homeassistant.components.binary_sensor import ( DEVICE_CLASS_PROBLEM, @@ -13,6 +13,7 @@ from homeassistant.components.binary_sensor import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback _LOGGER = logging.getLogger(__name__) @@ -21,8 +22,10 @@ DESCRIPTION_UNDER_VOLTAGE = "Under-voltage was detected. Consider getting a unin async def async_setup_entry( - hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities -): + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: """Set up rpi_power binary sensor.""" under_voltage = await hass.async_add_executor_job(new_under_voltage) async_add_entities([RaspberryChargerBinarySensor(under_voltage)], True) @@ -31,43 +34,21 @@ async def async_setup_entry( class RaspberryChargerBinarySensor(BinarySensorEntity): """Binary sensor representing the rpi power status.""" - def __init__(self, under_voltage): + _attr_device_class = DEVICE_CLASS_PROBLEM + _attr_icon = "mdi:raspberry-pi" + _attr_name = "RPi Power status" + _attr_unique_id = "rpi_power" # only one sensor possible + + def __init__(self, under_voltage: UnderVoltage) -> None: """Initialize the binary sensor.""" self._under_voltage = under_voltage - self._is_on = None - self._last_is_on = False - def update(self): + def update(self) -> None: """Update the state.""" - self._is_on = self._under_voltage.get() - if self._is_on != self._last_is_on: - if self._is_on: + value = self._under_voltage.get() + if self._attr_is_on != value: + if value: _LOGGER.warning(DESCRIPTION_UNDER_VOLTAGE) else: _LOGGER.info(DESCRIPTION_NORMALIZED) - self._last_is_on = self._is_on - - @property - def unique_id(self): - """Return the unique id of the sensor.""" - return "rpi_power" # only one sensor possible - - @property - def name(self): - """Return the name of the sensor.""" - return "RPi Power status" - - @property - def is_on(self): - """Return if there is a problem detected.""" - return self._is_on - - @property - def icon(self): - """Return the icon of the sensor.""" - return "mdi:raspberry-pi" - - @property - def device_class(self): - """Return the class of this device.""" - return DEVICE_CLASS_PROBLEM + self._attr_is_on = value diff --git a/homeassistant/components/rpi_power/config_flow.py b/homeassistant/components/rpi_power/config_flow.py index 1a038d05fdc..82457d5b296 100644 --- a/homeassistant/components/rpi_power/config_flow.py +++ b/homeassistant/components/rpi_power/config_flow.py @@ -35,7 +35,7 @@ class RPiPowerFlow(DiscoveryFlowHandler, domain=DOMAIN): self, data: dict[str, Any] | None = None ) -> FlowResult: """Handle a flow initialized by onboarding.""" - has_devices = await self._discovery_function(self.hass) + has_devices = await self._discovery_function(self.hass) # type: ignore if not has_devices: return self.async_abort(reason="no_devices_found") diff --git a/mypy.ini b/mypy.ini index 4e91295b597..cc491043c3a 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1012,6 +1012,17 @@ no_implicit_optional = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.rpi_power.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +no_implicit_optional = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.samsungtv.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -1695,9 +1706,6 @@ ignore_errors = true [mypy-homeassistant.components.ring.*] ignore_errors = true -[mypy-homeassistant.components.rpi_power.*] -ignore_errors = true - [mypy-homeassistant.components.ruckus_unleashed.*] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 375c55fe84b..d5542692e57 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -106,7 +106,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.profiler.*", "homeassistant.components.rachio.*", "homeassistant.components.ring.*", - "homeassistant.components.rpi_power.*", "homeassistant.components.ruckus_unleashed.*", "homeassistant.components.screenlogic.*", "homeassistant.components.search.*",