Fix tradfri fan typing

This commit is contained in:
Martin Hjelmare 2022-06-23 14:07:02 +02:00
parent e7cc87be4c
commit 8774d4ae75

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Any, cast from typing import Any
from pytradfri.api.aiocoap_api import APIRequestProtocol from pytradfri.api.aiocoap_api import APIRequestProtocol
@ -83,37 +83,30 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
gateway_id=gateway_id, gateway_id=gateway_id,
) )
self._device_control = self._device.air_purifier_control device_control = self._device.air_purifier_control
self._device_data = self._device_control.air_purifiers[0] assert (
device_control # air_purifier_control is ensured when creating the entity
)
self._device_control = device_control
self._device_data = device_control.air_purifiers[0]
def _refresh(self) -> None: def _refresh(self) -> None:
"""Refresh the device.""" """Refresh the device."""
self._device_data = self.coordinator.data.air_purifier_control.air_purifiers[0] self._device_data = self._device_control.air_purifiers[0]
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if switch is on.""" """Return true if switch is on."""
if not self._device_data: return self._device_data.state
return False
return cast(bool, self._device_data.state)
@property @property
def percentage(self) -> int | None: def percentage(self) -> int:
"""Return the current speed percentage.""" """Return the current speed percentage."""
if not self._device_data: return _from_fan_speed(self._device_data.fan_speed)
return None
if self._device_data.fan_speed:
return _from_fan_speed(self._device_data.fan_speed)
return None
@property @property
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
"""Return the current preset mode.""" """Return the current preset mode."""
if not self._device_data:
return None
if self._device_data.is_auto_mode: if self._device_data.is_auto_mode:
return ATTR_AUTO return ATTR_AUTO
@ -121,10 +114,8 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
async def async_set_preset_mode(self, preset_mode: str) -> None: async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan.""" """Set the preset mode of the fan."""
if not self._device_control: if not preset_mode == ATTR_AUTO:
return raise ValueError("Preset must be 'Auto'.")
# Preset must be 'Auto'
await self._api(self._device_control.turn_on_auto_mode()) await self._api(self._device_control.turn_on_auto_mode())
@ -135,9 +126,6 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Turn on the fan. Auto-mode if no argument is given.""" """Turn on the fan. Auto-mode if no argument is given."""
if not self._device_control:
return
if percentage is not None: if percentage is not None:
await self.async_set_percentage(percentage) await self.async_set_percentage(percentage)
return return
@ -147,9 +135,6 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
async def async_set_percentage(self, percentage: int) -> None: async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan.""" """Set the speed percentage of the fan."""
if not self._device_control:
return
if percentage == 0: if percentage == 0:
await self.async_turn_off() await self.async_turn_off()
return return
@ -160,6 +145,4 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan.""" """Turn off the fan."""
if not self._device_control:
return
await self._api(self._device_control.turn_off()) await self._api(self._device_control.turn_off())