From 64bfa20f6a6fb44255763abe47c304b4227db162 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 2 Jul 2022 19:15:54 +0200 Subject: [PATCH] Improve type hints in mqtt (#74295) --- homeassistant/components/mqtt/climate.py | 56 ++++++++++----------- homeassistant/components/mqtt/humidifier.py | 10 ++-- homeassistant/components/mqtt/mixins.py | 4 +- homeassistant/components/mqtt/number.py | 5 +- homeassistant/components/mqtt/select.py | 2 +- homeassistant/components/mqtt/sensor.py | 2 +- homeassistant/components/mqtt/siren.py | 8 +-- homeassistant/components/mqtt/switch.py | 15 ++++-- 8 files changed, 53 insertions(+), 49 deletions(-) diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index 6b09891483c..80ff33309ba 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -3,6 +3,7 @@ from __future__ import annotations import functools import logging +from typing import Any import voluptuous as vol @@ -754,29 +755,29 @@ class MqttClimate(MqttEntity, ClimateEntity): await subscription.async_subscribe_topics(self.hass, self._sub_state) @property - def temperature_unit(self): + def temperature_unit(self) -> str: """Return the unit of measurement.""" - if self._config.get(CONF_TEMPERATURE_UNIT): - return self._config.get(CONF_TEMPERATURE_UNIT) + if unit := self._config.get(CONF_TEMPERATURE_UNIT): + return unit return self.hass.config.units.temperature_unit @property - def current_temperature(self): + def current_temperature(self) -> float | None: """Return the current temperature.""" return self._current_temp @property - def target_temperature(self): + def target_temperature(self) -> float | None: """Return the temperature we try to reach.""" return self._target_temp @property - def target_temperature_low(self): + def target_temperature_low(self) -> float | None: """Return the low target temperature we try to reach.""" return self._target_temp_low @property - def target_temperature_high(self): + def target_temperature_high(self) -> float | None: """Return the high target temperature we try to reach.""" return self._target_temp_high @@ -796,7 +797,7 @@ class MqttClimate(MqttEntity, ClimateEntity): return self._config[CONF_MODE_LIST] @property - def target_temperature_step(self): + def target_temperature_step(self) -> float: """Return the supported step of target temperature.""" return self._config[CONF_TEMP_STEP] @@ -813,7 +814,7 @@ class MqttClimate(MqttEntity, ClimateEntity): return PRESET_NONE @property - def preset_modes(self) -> list: + def preset_modes(self) -> list[str]: """Return preset modes.""" presets = [] presets.extend(self._preset_modes) @@ -834,17 +835,17 @@ class MqttClimate(MqttEntity, ClimateEntity): return presets @property - def is_aux_heat(self): + def is_aux_heat(self) -> bool | None: """Return true if away mode is on.""" return self._aux @property - def fan_mode(self): + def fan_mode(self) -> str | None: """Return the fan setting.""" return self._current_fan_mode @property - def fan_modes(self): + def fan_modes(self) -> list[str]: """Return the list of available fan modes.""" return self._config[CONF_FAN_MODE_LIST] @@ -871,10 +872,9 @@ class MqttClimate(MqttEntity, ClimateEntity): payload = self._command_templates[cmnd_template](temp) await self._publish(cmnd_topic, payload) - async def async_set_temperature(self, **kwargs): + async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperatures.""" - if kwargs.get(ATTR_HVAC_MODE) is not None: - operation_mode = kwargs.get(ATTR_HVAC_MODE) + if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None: await self.async_set_hvac_mode(operation_mode) await self._set_temperature( @@ -904,7 +904,7 @@ class MqttClimate(MqttEntity, ClimateEntity): # Always optimistic? self.async_write_ha_state() - async def async_set_swing_mode(self, swing_mode): + async def async_set_swing_mode(self, swing_mode: str) -> None: """Set new swing mode.""" # CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 if self._send_if_off or self._current_operation != HVACMode.OFF: @@ -917,7 +917,7 @@ class MqttClimate(MqttEntity, ClimateEntity): self._current_swing_mode = swing_mode self.async_write_ha_state() - async def async_set_fan_mode(self, fan_mode): + async def async_set_fan_mode(self, fan_mode: str) -> None: """Set new target temperature.""" # CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 if self._send_if_off or self._current_operation != HVACMode.OFF: @@ -928,7 +928,7 @@ class MqttClimate(MqttEntity, ClimateEntity): self._current_fan_mode = fan_mode self.async_write_ha_state() - async def async_set_hvac_mode(self, hvac_mode) -> None: + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: """Set new operation mode.""" if hvac_mode == HVACMode.OFF: await self._publish( @@ -945,12 +945,12 @@ class MqttClimate(MqttEntity, ClimateEntity): self.async_write_ha_state() @property - def swing_mode(self): + def swing_mode(self) -> str | None: """Return the swing setting.""" return self._current_swing_mode @property - def swing_modes(self): + def swing_modes(self) -> list[str]: """List of available swing modes.""" return self._config[CONF_SWING_MODE_LIST] @@ -1027,16 +1027,16 @@ class MqttClimate(MqttEntity, ClimateEntity): self._aux = state self.async_write_ha_state() - async def async_turn_aux_heat_on(self): + async def async_turn_aux_heat_on(self) -> None: """Turn auxiliary heater on.""" await self._set_aux_heat(True) - async def async_turn_aux_heat_off(self): + async def async_turn_aux_heat_off(self) -> None: """Turn auxiliary heater off.""" await self._set_aux_heat(False) @property - def supported_features(self): + def supported_features(self) -> int: """Return the list of supported features.""" support = 0 @@ -1083,18 +1083,18 @@ class MqttClimate(MqttEntity, ClimateEntity): return support @property - def min_temp(self): + def min_temp(self) -> float: """Return the minimum temperature.""" return self._config[CONF_TEMP_MIN] @property - def max_temp(self): + def max_temp(self) -> float: """Return the maximum temperature.""" return self._config[CONF_TEMP_MAX] @property - def precision(self): + def precision(self) -> float: """Return the precision of the system.""" - if self._config.get(CONF_PRECISION) is not None: - return self._config.get(CONF_PRECISION) + if (precision := self._config.get(CONF_PRECISION)) is not None: + return precision return super().precision diff --git a/homeassistant/components/mqtt/humidifier.py b/homeassistant/components/mqtt/humidifier.py index 5f09fc0d513..43ff2af65d4 100644 --- a/homeassistant/components/mqtt/humidifier.py +++ b/homeassistant/components/mqtt/humidifier.py @@ -3,6 +3,7 @@ from __future__ import annotations import functools import logging +from typing import Any import voluptuous as vol @@ -407,7 +408,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity): await subscription.async_subscribe_topics(self.hass, self._sub_state) @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if we do optimistic updates.""" return self._optimistic @@ -431,10 +432,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity): """Return the current mode.""" return self._mode - async def async_turn_on( - self, - **kwargs, - ) -> None: + async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the entity. This method is a coroutine. @@ -451,7 +449,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity): self._state = True self.async_write_ha_state() - async def async_turn_off(self, **kwargs) -> None: + async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the entity. This method is a coroutine. diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index 8e59d09dfce..8fe9ee564de 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -355,7 +355,7 @@ class MqttAttributes(Entity): def __init__(self, config: dict) -> None: """Initialize the JSON attributes mixin.""" - self._attributes: dict | None = None + self._attributes: dict[str, Any] | None = None self._attributes_sub_state = None self._attributes_config = config @@ -426,7 +426,7 @@ class MqttAttributes(Entity): ) @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any] | None: """Return the state attributes.""" return self._attributes diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index dc27a740720..9df9dbf818c 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -12,6 +12,7 @@ from homeassistant.components.number import ( DEFAULT_MIN_VALUE, DEFAULT_STEP, DEVICE_CLASSES_SCHEMA, + NumberDeviceClass, RestoreNumber, ) from homeassistant.config_entries import ConfigEntry @@ -292,11 +293,11 @@ class MqttNumber(MqttEntity, RestoreNumber): ) @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if we do optimistic updates.""" return self._optimistic @property - def device_class(self) -> str | None: + def device_class(self) -> NumberDeviceClass | None: """Return the device class of the sensor.""" return self._config.get(CONF_DEVICE_CLASS) diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 4c302446b19..bdf55f895f3 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -211,6 +211,6 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity): ) @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if we do optimistic updates.""" return self._optimistic diff --git a/homeassistant/components/mqtt/sensor.py b/homeassistant/components/mqtt/sensor.py index 6948e173039..0dda382bec4 100644 --- a/homeassistant/components/mqtt/sensor.py +++ b/homeassistant/components/mqtt/sensor.py @@ -347,7 +347,7 @@ class MqttSensor(MqttEntity, RestoreSensor): return self._config.get(CONF_UNIT_OF_MEASUREMENT) @property - def force_update(self): + def force_update(self) -> bool: """Force update.""" return self._config[CONF_FORCE_UPDATE] diff --git a/homeassistant/components/mqtt/siren.py b/homeassistant/components/mqtt/siren.py index dfb89d2ee79..f1de0d27de7 100644 --- a/homeassistant/components/mqtt/siren.py +++ b/homeassistant/components/mqtt/siren.py @@ -309,12 +309,12 @@ class MqttSiren(MqttEntity, SirenEntity): await subscription.async_subscribe_topics(self.hass, self._sub_state) @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if we do optimistic updates.""" return self._optimistic @property - def extra_state_attributes(self) -> dict: + def extra_state_attributes(self) -> dict[str, Any]: """Return the state attributes.""" mqtt_attributes = super().extra_state_attributes attributes = ( @@ -353,7 +353,7 @@ class MqttSiren(MqttEntity, SirenEntity): self._config[CONF_ENCODING], ) - async def async_turn_on(self, **kwargs) -> None: + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the siren on. This method is a coroutine. @@ -371,7 +371,7 @@ class MqttSiren(MqttEntity, SirenEntity): self._update(kwargs) self.async_write_ha_state() - async def async_turn_off(self, **kwargs) -> None: + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the siren off. This method is a coroutine. diff --git a/homeassistant/components/mqtt/switch.py b/homeassistant/components/mqtt/switch.py index b04f2433659..34009695257 100644 --- a/homeassistant/components/mqtt/switch.py +++ b/homeassistant/components/mqtt/switch.py @@ -2,11 +2,16 @@ from __future__ import annotations import functools +from typing import Any import voluptuous as vol from homeassistant.components import switch -from homeassistant.components.switch import DEVICE_CLASSES_SCHEMA, SwitchEntity +from homeassistant.components.switch import ( + DEVICE_CLASSES_SCHEMA, + SwitchDeviceClass, + SwitchEntity, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_DEVICE_CLASS, @@ -195,16 +200,16 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity): return self._state @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if we do optimistic updates.""" return self._optimistic @property - def device_class(self) -> str | None: + def device_class(self) -> SwitchDeviceClass | None: """Return the device class of the sensor.""" return self._config.get(CONF_DEVICE_CLASS) - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on. This method is a coroutine. @@ -221,7 +226,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity): self._state = True self.async_write_ha_state() - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the device off. This method is a coroutine.