hass-core/homeassistant/components/intellifire/fan.py
Jeef 12336f5c15
Bump Intellifire to 4.1.9 (#121091)
* rebase

* Minor patch to fix duplicate DeviceInfo beign created - if data hasnt updated yet

* rebase

* Minor patch to fix duplicate DeviceInfo beign created - if data hasnt updated yet

* fixing formatting

* Update homeassistant/components/intellifire/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/intellifire/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Removing cloud connectivity sensor - leaving local one in

* Renaming class to something more useful

* addressing pr

* Update homeassistant/components/intellifire/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* add ruff exception

* Fix test annotations

* remove access to private variable

* Bumping to 4.1.9 instead of 4.1.5

* A renaming

* rename

* Updated testing

* Update __init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* updateing styrings

* Update tests/components/intellifire/conftest.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Testing refactor - WIP

* everything is passing - cleanup still needed

* cleaning up comments

* update pr

* unrename

* Update homeassistant/components/intellifire/coordinator.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* fixing sentence

* fixed fixture and removed error codes

* reverted a bad change

* fixing strings.json

* revert renaming

* fix

* typing inother pr

* adding extra tests - one has a really dumb name

* using a real value

* added a migration in

* Update homeassistant/components/intellifire/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update tests/components/intellifire/test_init.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* cleanup continues

* addressing pr

* switch back to debug

* Update tests/components/intellifire/conftest.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* some changes

* restore property mock cuase didnt work otherwise

* cleanup has begun

* removed extra text

* addressing pr stuff

* fixed reauth

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-09-01 12:48:38 +02:00

136 lines
4.2 KiB
Python

"""Fan definition for Intellifire."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
import math
from typing import Any
from intellifire4py.control import IntelliFireController
from intellifire4py.model import IntelliFirePollData
from homeassistant.components.fan import (
FanEntity,
FanEntityDescription,
FanEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.percentage import (
percentage_to_ranged_value,
ranged_value_to_percentage,
)
from .const import DOMAIN, LOGGER
from .coordinator import IntellifireDataUpdateCoordinator
from .entity import IntellifireEntity
@dataclass(frozen=True)
class IntellifireFanRequiredKeysMixin:
"""Required keys for fan entity."""
set_fn: Callable[[IntelliFireController, int], Awaitable]
value_fn: Callable[[IntelliFirePollData], int]
speed_range: tuple[int, int]
@dataclass(frozen=True)
class IntellifireFanEntityDescription(
FanEntityDescription, IntellifireFanRequiredKeysMixin
):
"""Describes a fan entity."""
INTELLIFIRE_FANS: tuple[IntellifireFanEntityDescription, ...] = (
IntellifireFanEntityDescription(
key="fan",
translation_key="fan",
set_fn=lambda control_api, speed: control_api.set_fan_speed(speed=speed),
value_fn=lambda data: data.fanspeed,
speed_range=(1, 4),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the fans."""
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
if coordinator.data.has_fan:
async_add_entities(
IntellifireFan(coordinator=coordinator, description=description)
for description in INTELLIFIRE_FANS
)
return
LOGGER.debug("Disabling Fan - IntelliFire device does not appear to have one")
class IntellifireFan(IntellifireEntity, FanEntity):
"""Fan entity for the fireplace."""
entity_description: IntellifireFanEntityDescription
_attr_supported_features = (
FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
_enable_turn_on_off_backwards_compatibility = False
@property
def is_on(self) -> bool:
"""Return on or off."""
return self.entity_description.value_fn(self.coordinator.read_api.data) >= 1
@property
def percentage(self) -> int | None:
"""Return fan percentage."""
return ranged_value_to_percentage(
self.entity_description.speed_range,
self.coordinator.read_api.data.fanspeed,
)
@property
def speed_count(self) -> int:
"""Count of supported speeds."""
return self.entity_description.speed_range[1]
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
# Calculate percentage steps
LOGGER.debug("Setting Fan Speed %s", percentage)
int_value = math.ceil(
percentage_to_ranged_value(self.entity_description.speed_range, percentage)
)
await self.entity_description.set_fn(self.coordinator.control_api, int_value)
await self.coordinator.async_request_refresh()
async def async_turn_on(
self,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
) -> None:
"""Turn on the fan."""
if percentage:
int_value = math.ceil(
percentage_to_ranged_value(
self.entity_description.speed_range, percentage
)
)
else:
int_value = 1
await self.entity_description.set_fn(self.coordinator.control_api, int_value)
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan."""
await self.entity_description.set_fn(self.coordinator.control_api, 0)
await self.coordinator.async_request_refresh()