core/homeassistant/components/intellifire/light.py

100 lines
3.1 KiB
Python
Raw Normal View History

2023-01-09 22:16:39 +00:00
"""The IntelliFire Light."""
2023-01-09 22:16:39 +00:00
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
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 10:48:38 +00:00
from intellifire4py.control import IntelliFireController
from intellifire4py.model import IntelliFirePollData
2023-01-09 22:16:39 +00:00
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ColorMode,
LightEntity,
LightEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2023-01-09 22:16:39 +00:00
from .const import DOMAIN, LOGGER
2023-01-09 22:16:39 +00:00
from .coordinator import IntellifireDataUpdateCoordinator
from .entity import IntellifireEntity
@dataclass(frozen=True)
2023-01-09 22:16:39 +00:00
class IntellifireLightRequiredKeysMixin:
"""Required keys for fan entity."""
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 10:48:38 +00:00
set_fn: Callable[[IntelliFireController, int], Awaitable]
value_fn: Callable[[IntelliFirePollData], int]
2023-01-09 22:16:39 +00:00
@dataclass(frozen=True)
2023-01-09 22:16:39 +00:00
class IntellifireLightEntityDescription(
LightEntityDescription, IntellifireLightRequiredKeysMixin
):
"""Describes a light entity."""
INTELLIFIRE_LIGHTS: tuple[IntellifireLightEntityDescription, ...] = (
IntellifireLightEntityDescription(
key="lights",
translation_key="lights",
2023-01-09 22:16:39 +00:00
set_fn=lambda control_api, level: control_api.set_lights(level=level),
value_fn=lambda data: data.light_level,
),
)
class IntellifireLight(IntellifireEntity, LightEntity):
"""Light entity for the fireplace."""
2023-01-09 22:16:39 +00:00
entity_description: IntellifireLightEntityDescription
_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
@property
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 10:48:38 +00:00
def brightness(self) -> int:
2023-01-09 22:16:39 +00:00
"""Return the current brightness 0-255."""
return 85 * self.entity_description.value_fn(self.coordinator.read_api.data)
@property
def is_on(self):
"""Return true if light is on."""
return self.entity_description.value_fn(self.coordinator.read_api.data) >= 1
async def async_turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
if ATTR_BRIGHTNESS in kwargs:
light_level = int(kwargs[ATTR_BRIGHTNESS] / 85)
else:
light_level = 2
await self.entity_description.set_fn(self.coordinator.control_api, light_level)
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
await self.entity_description.set_fn(self.coordinator.control_api, 0)
await self.coordinator.async_request_refresh()
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
2023-01-09 22:16:39 +00:00
) -> None:
"""Set up the fans."""
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
if coordinator.data.has_light:
async_add_entities(
IntellifireLight(coordinator=coordinator, description=description)
for description in INTELLIFIRE_LIGHTS
)
return
LOGGER.debug("Disabling Lights - IntelliFire device does not appear to have one")