From c8400261983412abd87b2192c613c2829778cfde Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 26 Aug 2022 10:52:57 +0200 Subject: [PATCH] Improve type hint in fibaro climate entity (#77153) --- homeassistant/components/fibaro/climate.py | 83 +++++++--------------- 1 file changed, 25 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/fibaro/climate.py b/homeassistant/components/fibaro/climate.py index a2d322528db5..94dd599698de 100644 --- a/homeassistant/components/fibaro/climate.py +++ b/homeassistant/components/fibaro/climate.py @@ -121,15 +121,12 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): def __init__(self, fibaro_device): """Initialize the Fibaro device.""" super().__init__(fibaro_device) - self._temp_sensor_device = None - self._target_temp_device = None - self._op_mode_device = None - self._fan_mode_device = None - self._support_flags = 0 + self._temp_sensor_device: FibaroDevice | None = None + self._target_temp_device: FibaroDevice | None = None + self._op_mode_device: FibaroDevice | None = None + self._fan_mode_device: FibaroDevice | None = None + self._attr_supported_features = 0 self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id) - self._hvac_support = [] - self._preset_support = [] - self._fan_support = [] siblings = fibaro_device.fibaro_controller.get_siblings(fibaro_device) _LOGGER.debug("%s siblings: %s", fibaro_device.ha_id, siblings) @@ -158,34 +155,38 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): or "setHeatingThermostatSetpoint" in device.actions ): self._target_temp_device = FibaroDevice(device) - self._support_flags |= ClimateEntityFeature.TARGET_TEMPERATURE + self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE tempunit = device.properties.unit if "setMode" in device.actions or "setOperatingMode" in device.actions: self._op_mode_device = FibaroDevice(device) - self._support_flags |= ClimateEntityFeature.PRESET_MODE + self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE if "setFanMode" in device.actions: self._fan_mode_device = FibaroDevice(device) - self._support_flags |= ClimateEntityFeature.FAN_MODE + self._attr_supported_features |= ClimateEntityFeature.FAN_MODE if tempunit == "F": - self._unit_of_temp = TEMP_FAHRENHEIT + self._attr_temperature_unit = TEMP_FAHRENHEIT else: - self._unit_of_temp = TEMP_CELSIUS + self._attr_temperature_unit = TEMP_CELSIUS if self._fan_mode_device: fan_modes = ( self._fan_mode_device.fibaro_device.properties.supportedModes.split(",") ) + self._attr_fan_modes = [] for mode in fan_modes: mode = int(mode) if mode not in FANMODES: _LOGGER.warning("%d unknown fan mode", mode) continue - self._fan_support.append(FANMODES[int(mode)]) + self._attr_fan_modes.append(FANMODES[int(mode)]) + self._attr_hvac_modes = [HVACMode.AUTO] # default if self._op_mode_device: + self._attr_preset_modes = [] + self._attr_hvac_modes = [] prop = self._op_mode_device.fibaro_device.properties if "supportedOperatingModes" in prop: op_modes = prop.supportedOperatingModes.split(",") @@ -195,10 +196,10 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): mode = int(mode) if mode in OPMODES_HVAC: mode_ha = OPMODES_HVAC[mode] - if mode_ha not in self._hvac_support: - self._hvac_support.append(mode_ha) + if mode_ha not in self._attr_hvac_modes: + self._attr_hvac_modes.append(mode_ha) if mode in OPMODES_PRESET: - self._preset_support.append(OPMODES_PRESET[mode]) + self._attr_preset_modes.append(OPMODES_PRESET[mode]) async def async_added_to_hass(self) -> None: """Call when entity is added to hass.""" @@ -223,19 +224,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): self.controller.register(device.id, self._update_callback) @property - def supported_features(self): - """Return the list of supported features.""" - return self._support_flags - - @property - def fan_modes(self): - """Return the list of available fan modes.""" - if not self._fan_mode_device: - return None - return self._fan_support - - @property - def fan_mode(self): + def fan_mode(self) -> str | None: """Return the fan setting.""" if not self._fan_mode_device: return None @@ -249,7 +238,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): self._fan_mode_device.action("setFanMode", HA_FANMODES[fan_mode]) @property - def fibaro_op_mode(self): + def fibaro_op_mode(self) -> int: """Return the operating mode of the device.""" if not self._op_mode_device: return 3 # Default to AUTO @@ -260,17 +249,10 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): return int(self._op_mode_device.fibaro_device.properties.mode) @property - def hvac_mode(self): + def hvac_mode(self) -> HVACMode: """Return current operation ie. heat, cool, idle.""" return OPMODES_HVAC[self.fibaro_op_mode] - @property - def hvac_modes(self): - """Return the list of available operation modes.""" - if not self._op_mode_device: - return [HVACMode.AUTO] # Default to this - return self._hvac_support - def set_hvac_mode(self, hvac_mode: HVACMode) -> None: """Set new target operation mode.""" if not self._op_mode_device: @@ -284,7 +266,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): self._op_mode_device.action("setMode", HA_OPMODES_HVAC[hvac_mode]) @property - def preset_mode(self): + def preset_mode(self) -> str | None: """Return the current preset mode, e.g., home, away, temp. Requires ClimateEntityFeature.PRESET_MODE. @@ -301,16 +283,6 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): return None return OPMODES_PRESET[mode] - @property - def preset_modes(self): - """Return a list of available preset modes. - - Requires ClimateEntityFeature.PRESET_MODE. - """ - if not self._op_mode_device: - return None - return self._preset_support - def set_preset_mode(self, preset_mode: str) -> None: """Set new preset mode.""" if self._op_mode_device is None: @@ -323,12 +295,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): self._op_mode_device.action("setMode", HA_OPMODES_PRESET[preset_mode]) @property - def temperature_unit(self): - """Return the unit of measurement.""" - return self._unit_of_temp - - @property - def current_temperature(self): + def current_temperature(self) -> float | None: """Return the current temperature.""" if self._temp_sensor_device: device = self._temp_sensor_device.fibaro_device @@ -338,7 +305,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): return None @property - def target_temperature(self): + def target_temperature(self) -> float | None: """Return the temperature we try to reach.""" if self._target_temp_device: device = self._target_temp_device.fibaro_device @@ -351,7 +318,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity): """Set new target temperatures.""" temperature = kwargs.get(ATTR_TEMPERATURE) target = self._target_temp_device - if temperature is not None: + if target is not None and temperature is not None: if "setThermostatSetpoint" in target.fibaro_device.actions: target.action("setThermostatSetpoint", self.fibaro_op_mode, temperature) elif "setHeatingThermostatSetpoint" in target.fibaro_device.actions: