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