Improve type hints in mqtt (#74295)

This commit is contained in:
epenet 2022-07-02 19:15:54 +02:00 committed by GitHub
parent 6f67ae1dfc
commit 64bfa20f6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 49 deletions

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import functools import functools
import logging import logging
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -754,29 +755,29 @@ class MqttClimate(MqttEntity, ClimateEntity):
await subscription.async_subscribe_topics(self.hass, self._sub_state) await subscription.async_subscribe_topics(self.hass, self._sub_state)
@property @property
def temperature_unit(self): def temperature_unit(self) -> str:
"""Return the unit of measurement.""" """Return the unit of measurement."""
if self._config.get(CONF_TEMPERATURE_UNIT): if unit := self._config.get(CONF_TEMPERATURE_UNIT):
return self._config.get(CONF_TEMPERATURE_UNIT) return unit
return self.hass.config.units.temperature_unit return self.hass.config.units.temperature_unit
@property @property
def current_temperature(self): def current_temperature(self) -> float | None:
"""Return the current temperature.""" """Return the current temperature."""
return self._current_temp return self._current_temp
@property @property
def target_temperature(self): def target_temperature(self) -> float | None:
"""Return the temperature we try to reach.""" """Return the temperature we try to reach."""
return self._target_temp return self._target_temp
@property @property
def target_temperature_low(self): def target_temperature_low(self) -> float | None:
"""Return the low target temperature we try to reach.""" """Return the low target temperature we try to reach."""
return self._target_temp_low return self._target_temp_low
@property @property
def target_temperature_high(self): def target_temperature_high(self) -> float | None:
"""Return the high target temperature we try to reach.""" """Return the high target temperature we try to reach."""
return self._target_temp_high return self._target_temp_high
@ -796,7 +797,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
return self._config[CONF_MODE_LIST] return self._config[CONF_MODE_LIST]
@property @property
def target_temperature_step(self): def target_temperature_step(self) -> float:
"""Return the supported step of target temperature.""" """Return the supported step of target temperature."""
return self._config[CONF_TEMP_STEP] return self._config[CONF_TEMP_STEP]
@ -813,7 +814,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
return PRESET_NONE return PRESET_NONE
@property @property
def preset_modes(self) -> list: def preset_modes(self) -> list[str]:
"""Return preset modes.""" """Return preset modes."""
presets = [] presets = []
presets.extend(self._preset_modes) presets.extend(self._preset_modes)
@ -834,17 +835,17 @@ class MqttClimate(MqttEntity, ClimateEntity):
return presets return presets
@property @property
def is_aux_heat(self): def is_aux_heat(self) -> bool | None:
"""Return true if away mode is on.""" """Return true if away mode is on."""
return self._aux return self._aux
@property @property
def fan_mode(self): def fan_mode(self) -> str | None:
"""Return the fan setting.""" """Return the fan setting."""
return self._current_fan_mode return self._current_fan_mode
@property @property
def fan_modes(self): def fan_modes(self) -> list[str]:
"""Return the list of available fan modes.""" """Return the list of available fan modes."""
return self._config[CONF_FAN_MODE_LIST] return self._config[CONF_FAN_MODE_LIST]
@ -871,10 +872,9 @@ class MqttClimate(MqttEntity, ClimateEntity):
payload = self._command_templates[cmnd_template](temp) payload = self._command_templates[cmnd_template](temp)
await self._publish(cmnd_topic, payload) 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.""" """Set new target temperatures."""
if kwargs.get(ATTR_HVAC_MODE) is not None: if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None:
operation_mode = kwargs.get(ATTR_HVAC_MODE)
await self.async_set_hvac_mode(operation_mode) await self.async_set_hvac_mode(operation_mode)
await self._set_temperature( await self._set_temperature(
@ -904,7 +904,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
# Always optimistic? # Always optimistic?
self.async_write_ha_state() 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.""" """Set new swing mode."""
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 # 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: 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._current_swing_mode = swing_mode
self.async_write_ha_state() 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.""" """Set new target temperature."""
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 # 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: 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._current_fan_mode = fan_mode
self.async_write_ha_state() 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.""" """Set new operation mode."""
if hvac_mode == HVACMode.OFF: if hvac_mode == HVACMode.OFF:
await self._publish( await self._publish(
@ -945,12 +945,12 @@ class MqttClimate(MqttEntity, ClimateEntity):
self.async_write_ha_state() self.async_write_ha_state()
@property @property
def swing_mode(self): def swing_mode(self) -> str | None:
"""Return the swing setting.""" """Return the swing setting."""
return self._current_swing_mode return self._current_swing_mode
@property @property
def swing_modes(self): def swing_modes(self) -> list[str]:
"""List of available swing modes.""" """List of available swing modes."""
return self._config[CONF_SWING_MODE_LIST] return self._config[CONF_SWING_MODE_LIST]
@ -1027,16 +1027,16 @@ class MqttClimate(MqttEntity, ClimateEntity):
self._aux = state self._aux = state
self.async_write_ha_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.""" """Turn auxiliary heater on."""
await self._set_aux_heat(True) 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.""" """Turn auxiliary heater off."""
await self._set_aux_heat(False) await self._set_aux_heat(False)
@property @property
def supported_features(self): def supported_features(self) -> int:
"""Return the list of supported features.""" """Return the list of supported features."""
support = 0 support = 0
@ -1083,18 +1083,18 @@ class MqttClimate(MqttEntity, ClimateEntity):
return support return support
@property @property
def min_temp(self): def min_temp(self) -> float:
"""Return the minimum temperature.""" """Return the minimum temperature."""
return self._config[CONF_TEMP_MIN] return self._config[CONF_TEMP_MIN]
@property @property
def max_temp(self): def max_temp(self) -> float:
"""Return the maximum temperature.""" """Return the maximum temperature."""
return self._config[CONF_TEMP_MAX] return self._config[CONF_TEMP_MAX]
@property @property
def precision(self): def precision(self) -> float:
"""Return the precision of the system.""" """Return the precision of the system."""
if self._config.get(CONF_PRECISION) is not None: if (precision := self._config.get(CONF_PRECISION)) is not None:
return self._config.get(CONF_PRECISION) return precision
return super().precision return super().precision

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import functools import functools
import logging import logging
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -407,7 +408,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
await subscription.async_subscribe_topics(self.hass, self._sub_state) await subscription.async_subscribe_topics(self.hass, self._sub_state)
@property @property
def assumed_state(self): def assumed_state(self) -> bool:
"""Return true if we do optimistic updates.""" """Return true if we do optimistic updates."""
return self._optimistic return self._optimistic
@ -431,10 +432,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
"""Return the current mode.""" """Return the current mode."""
return self._mode return self._mode
async def async_turn_on( async def async_turn_on(self, **kwargs: Any) -> None:
self,
**kwargs,
) -> None:
"""Turn on the entity. """Turn on the entity.
This method is a coroutine. This method is a coroutine.
@ -451,7 +449,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
self._state = True self._state = True
self.async_write_ha_state() 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. """Turn off the entity.
This method is a coroutine. This method is a coroutine.

View file

@ -355,7 +355,7 @@ class MqttAttributes(Entity):
def __init__(self, config: dict) -> None: def __init__(self, config: dict) -> None:
"""Initialize the JSON attributes mixin.""" """Initialize the JSON attributes mixin."""
self._attributes: dict | None = None self._attributes: dict[str, Any] | None = None
self._attributes_sub_state = None self._attributes_sub_state = None
self._attributes_config = config self._attributes_config = config
@ -426,7 +426,7 @@ class MqttAttributes(Entity):
) )
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes.""" """Return the state attributes."""
return self._attributes return self._attributes

View file

@ -12,6 +12,7 @@ from homeassistant.components.number import (
DEFAULT_MIN_VALUE, DEFAULT_MIN_VALUE,
DEFAULT_STEP, DEFAULT_STEP,
DEVICE_CLASSES_SCHEMA, DEVICE_CLASSES_SCHEMA,
NumberDeviceClass,
RestoreNumber, RestoreNumber,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -292,11 +293,11 @@ class MqttNumber(MqttEntity, RestoreNumber):
) )
@property @property
def assumed_state(self): def assumed_state(self) -> bool:
"""Return true if we do optimistic updates.""" """Return true if we do optimistic updates."""
return self._optimistic return self._optimistic
@property @property
def device_class(self) -> str | None: def device_class(self) -> NumberDeviceClass | None:
"""Return the device class of the sensor.""" """Return the device class of the sensor."""
return self._config.get(CONF_DEVICE_CLASS) return self._config.get(CONF_DEVICE_CLASS)

View file

@ -211,6 +211,6 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
) )
@property @property
def assumed_state(self): def assumed_state(self) -> bool:
"""Return true if we do optimistic updates.""" """Return true if we do optimistic updates."""
return self._optimistic return self._optimistic

View file

@ -347,7 +347,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
return self._config.get(CONF_UNIT_OF_MEASUREMENT) return self._config.get(CONF_UNIT_OF_MEASUREMENT)
@property @property
def force_update(self): def force_update(self) -> bool:
"""Force update.""" """Force update."""
return self._config[CONF_FORCE_UPDATE] return self._config[CONF_FORCE_UPDATE]

View file

@ -309,12 +309,12 @@ class MqttSiren(MqttEntity, SirenEntity):
await subscription.async_subscribe_topics(self.hass, self._sub_state) await subscription.async_subscribe_topics(self.hass, self._sub_state)
@property @property
def assumed_state(self): def assumed_state(self) -> bool:
"""Return true if we do optimistic updates.""" """Return true if we do optimistic updates."""
return self._optimistic return self._optimistic
@property @property
def extra_state_attributes(self) -> dict: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes.""" """Return the state attributes."""
mqtt_attributes = super().extra_state_attributes mqtt_attributes = super().extra_state_attributes
attributes = ( attributes = (
@ -353,7 +353,7 @@ class MqttSiren(MqttEntity, SirenEntity):
self._config[CONF_ENCODING], 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. """Turn the siren on.
This method is a coroutine. This method is a coroutine.
@ -371,7 +371,7 @@ class MqttSiren(MqttEntity, SirenEntity):
self._update(kwargs) self._update(kwargs)
self.async_write_ha_state() 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. """Turn the siren off.
This method is a coroutine. This method is a coroutine.

View file

@ -2,11 +2,16 @@
from __future__ import annotations from __future__ import annotations
import functools import functools
from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.components import switch 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.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_DEVICE_CLASS, CONF_DEVICE_CLASS,
@ -195,16 +200,16 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
return self._state return self._state
@property @property
def assumed_state(self): def assumed_state(self) -> bool:
"""Return true if we do optimistic updates.""" """Return true if we do optimistic updates."""
return self._optimistic return self._optimistic
@property @property
def device_class(self) -> str | None: def device_class(self) -> SwitchDeviceClass | None:
"""Return the device class of the sensor.""" """Return the device class of the sensor."""
return self._config.get(CONF_DEVICE_CLASS) 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. """Turn the device on.
This method is a coroutine. This method is a coroutine.
@ -221,7 +226,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
self._state = True self._state = True
self.async_write_ha_state() 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. """Turn the device off.
This method is a coroutine. This method is a coroutine.