mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Add support for percentage based fan model in esphome (#46712)
This commit is contained in:
parent
bdc8a2878f
commit
ecf93e09e8
@ -1,4 +1,5 @@
|
|||||||
"""Support for ESPHome fans."""
|
"""Support for ESPHome fans."""
|
||||||
|
import math
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
||||||
@ -16,6 +17,8 @@ from homeassistant.helpers.typing import HomeAssistantType
|
|||||||
from homeassistant.util.percentage import (
|
from homeassistant.util.percentage import (
|
||||||
ordered_list_item_to_percentage,
|
ordered_list_item_to_percentage,
|
||||||
percentage_to_ordered_list_item,
|
percentage_to_ordered_list_item,
|
||||||
|
percentage_to_ranged_value,
|
||||||
|
ranged_value_to_percentage,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
@ -62,6 +65,11 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
def _state(self) -> Optional[FanState]:
|
def _state(self) -> Optional[FanState]:
|
||||||
return super()._state
|
return super()._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _supports_speed_levels(self) -> bool:
|
||||||
|
api_version = self._client.api_version
|
||||||
|
return api_version.major == 1 and api_version.minor > 3
|
||||||
|
|
||||||
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 percentage == 0:
|
if percentage == 0:
|
||||||
@ -70,6 +78,13 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
|
|
||||||
data = {"key": self._static_info.key, "state": True}
|
data = {"key": self._static_info.key, "state": True}
|
||||||
if percentage is not None:
|
if percentage is not None:
|
||||||
|
if self._supports_speed_levels:
|
||||||
|
data["speed_level"] = math.ceil(
|
||||||
|
percentage_to_ranged_value(
|
||||||
|
(1, self._static_info.supported_speed_levels), percentage
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
named_speed = percentage_to_ordered_list_item(
|
named_speed = percentage_to_ordered_list_item(
|
||||||
ORDERED_NAMED_FAN_SPEEDS, percentage
|
ORDERED_NAMED_FAN_SPEEDS, percentage
|
||||||
)
|
)
|
||||||
@ -115,14 +130,22 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
"""Return the current speed percentage."""
|
"""Return the current speed percentage."""
|
||||||
if not self._static_info.supports_speed:
|
if not self._static_info.supports_speed:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if not self._supports_speed_levels:
|
||||||
return ordered_list_item_to_percentage(
|
return ordered_list_item_to_percentage(
|
||||||
ORDERED_NAMED_FAN_SPEEDS, self._state.speed
|
ORDERED_NAMED_FAN_SPEEDS, self._state.speed
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return ranged_value_to_percentage(
|
||||||
|
(1, self._static_info.supported_speed_levels), self._state.speed_level
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def speed_count(self) -> int:
|
def speed_count(self) -> int:
|
||||||
"""Return the number of speeds the fan supports."""
|
"""Return the number of speeds the fan supports."""
|
||||||
|
if not self._supports_speed_levels:
|
||||||
return len(ORDERED_NAMED_FAN_SPEEDS)
|
return len(ORDERED_NAMED_FAN_SPEEDS)
|
||||||
|
return self._static_info.supported_speed_levels
|
||||||
|
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
def oscillating(self) -> None:
|
def oscillating(self) -> None:
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"name": "ESPHome",
|
"name": "ESPHome",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/esphome",
|
"documentation": "https://www.home-assistant.io/integrations/esphome",
|
||||||
"requirements": ["aioesphomeapi==2.6.5"],
|
"requirements": ["aioesphomeapi==2.6.6"],
|
||||||
"zeroconf": ["_esphomelib._tcp.local."],
|
"zeroconf": ["_esphomelib._tcp.local."],
|
||||||
"codeowners": ["@OttoWinter"],
|
"codeowners": ["@OttoWinter"],
|
||||||
"after_dependencies": ["zeroconf", "tag"]
|
"after_dependencies": ["zeroconf", "tag"]
|
||||||
|
@ -154,7 +154,7 @@ aiodns==2.0.0
|
|||||||
aioeafm==0.1.2
|
aioeafm==0.1.2
|
||||||
|
|
||||||
# homeassistant.components.esphome
|
# homeassistant.components.esphome
|
||||||
aioesphomeapi==2.6.5
|
aioesphomeapi==2.6.6
|
||||||
|
|
||||||
# homeassistant.components.flo
|
# homeassistant.components.flo
|
||||||
aioflo==0.4.1
|
aioflo==0.4.1
|
||||||
|
@ -91,7 +91,7 @@ aiodns==2.0.0
|
|||||||
aioeafm==0.1.2
|
aioeafm==0.1.2
|
||||||
|
|
||||||
# homeassistant.components.esphome
|
# homeassistant.components.esphome
|
||||||
aioesphomeapi==2.6.5
|
aioesphomeapi==2.6.6
|
||||||
|
|
||||||
# homeassistant.components.flo
|
# homeassistant.components.flo
|
||||||
aioflo==0.4.1
|
aioflo==0.4.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user