mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add a base class for Intellifire entities (#65077)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
parent
13af2728c2
commit
d479949ca2
@ -547,6 +547,7 @@ omit =
|
|||||||
homeassistant/components/intellifire/coordinator.py
|
homeassistant/components/intellifire/coordinator.py
|
||||||
homeassistant/components/intellifire/binary_sensor.py
|
homeassistant/components/intellifire/binary_sensor.py
|
||||||
homeassistant/components/intellifire/sensor.py
|
homeassistant/components/intellifire/sensor.py
|
||||||
|
homeassistant/components/intellifire/entity.py
|
||||||
homeassistant/components/incomfort/*
|
homeassistant/components/incomfort/*
|
||||||
homeassistant/components/intesishome/*
|
homeassistant/components/intesishome/*
|
||||||
homeassistant/components/ios/__init__.py
|
homeassistant/components/ios/__init__.py
|
||||||
|
@ -13,10 +13,10 @@ from homeassistant.components.binary_sensor import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from . import IntellifireDataUpdateCoordinator
|
from . import IntellifireDataUpdateCoordinator
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import IntellifireEntity
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -75,28 +75,11 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class IntellifireBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
class IntellifireBinarySensor(IntellifireEntity, BinarySensorEntity):
|
||||||
"""A semi-generic wrapper around Binary Sensor entities for IntelliFire."""
|
"""Extends IntellifireEntity with Binary Sensor specific logic."""
|
||||||
|
|
||||||
# Define types
|
|
||||||
coordinator: IntellifireDataUpdateCoordinator
|
|
||||||
entity_description: IntellifireBinarySensorEntityDescription
|
entity_description: IntellifireBinarySensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
coordinator: IntellifireDataUpdateCoordinator,
|
|
||||||
description: IntellifireBinarySensorEntityDescription,
|
|
||||||
) -> None:
|
|
||||||
"""Class initializer."""
|
|
||||||
super().__init__(coordinator=coordinator)
|
|
||||||
self.entity_description = description
|
|
||||||
|
|
||||||
# Set the Display name the User will see
|
|
||||||
self._attr_name = f"Fireplace {description.name}"
|
|
||||||
self._attr_unique_id = f"{description.key}_{coordinator.api.data.serial}"
|
|
||||||
# Configure the Device Info
|
|
||||||
self._attr_device_info = self.coordinator.device_info
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Use this to get the correct value."""
|
"""Use this to get the correct value."""
|
||||||
|
28
homeassistant/components/intellifire/entity.py
Normal file
28
homeassistant/components/intellifire/entity.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Platform for shared base classes for sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from . import IntellifireDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
|
class IntellifireEntity(CoordinatorEntity):
|
||||||
|
"""Define a generic class for Intellifire entities."""
|
||||||
|
|
||||||
|
coordinator: IntellifireDataUpdateCoordinator
|
||||||
|
_attr_attribution = "Data provided by unpublished Intellifire API"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: IntellifireDataUpdateCoordinator,
|
||||||
|
description: EntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Class initializer."""
|
||||||
|
super().__init__(coordinator=coordinator)
|
||||||
|
self.entity_description = description
|
||||||
|
# Set the Display name the User will see
|
||||||
|
self._attr_name = f"Fireplace {description.name}"
|
||||||
|
self._attr_unique_id = f"{description.key}_{coordinator.api.data.serial}"
|
||||||
|
# Configure the Device Info
|
||||||
|
self._attr_device_info = self.coordinator.device_info
|
@ -17,47 +17,11 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.const import TEMP_CELSIUS
|
from homeassistant.const import TEMP_CELSIUS
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from . import IntellifireDataUpdateCoordinator
|
from . import IntellifireDataUpdateCoordinator
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import IntellifireEntity
|
||||||
|
|
||||||
class IntellifireSensor(CoordinatorEntity, SensorEntity):
|
|
||||||
"""Define a generic class for Sensors."""
|
|
||||||
|
|
||||||
# Define types
|
|
||||||
coordinator: IntellifireDataUpdateCoordinator
|
|
||||||
entity_description: IntellifireSensorEntityDescription
|
|
||||||
_attr_attribution = "Data provided by unpublished Intellifire API"
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
coordinator: IntellifireDataUpdateCoordinator,
|
|
||||||
description: IntellifireSensorEntityDescription,
|
|
||||||
) -> None:
|
|
||||||
"""Init the sensor."""
|
|
||||||
super().__init__(coordinator=coordinator)
|
|
||||||
self.entity_description = description
|
|
||||||
|
|
||||||
# Set the Display name the User will see
|
|
||||||
self._attr_name = f"Fireplace {description.name}"
|
|
||||||
self._attr_unique_id = f"{description.key}_{coordinator.api.data.serial}"
|
|
||||||
# Configure the Device Info
|
|
||||||
self._attr_device_info = self.coordinator.device_info
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self) -> int | str | datetime | None:
|
|
||||||
"""Return the state."""
|
|
||||||
return self.entity_description.value_fn(self.coordinator.api.data)
|
|
||||||
|
|
||||||
|
|
||||||
def _time_remaining_to_timestamp(data: IntellifirePollData) -> datetime | None:
|
|
||||||
"""Define a sensor that takes into account timezone."""
|
|
||||||
if not (seconds_offset := data.timeremaining_s):
|
|
||||||
return None
|
|
||||||
return utcnow() + timedelta(seconds=seconds_offset)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -69,21 +33,17 @@ class IntellifireSensorRequiredKeysMixin:
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class IntellifireSensorEntityDescription(
|
class IntellifireSensorEntityDescription(
|
||||||
SensorEntityDescription, IntellifireSensorRequiredKeysMixin
|
SensorEntityDescription,
|
||||||
|
IntellifireSensorRequiredKeysMixin,
|
||||||
):
|
):
|
||||||
"""Describes a sensor sensor entity."""
|
"""Describes a sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
def _time_remaining_to_timestamp(data: IntellifirePollData) -> datetime | None:
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
"""Define a sensor that takes into account timezone."""
|
||||||
) -> None:
|
if not (seconds_offset := data.timeremaining_s):
|
||||||
"""Define setup entry call."""
|
return None
|
||||||
|
return utcnow() + timedelta(seconds=seconds_offset)
|
||||||
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
async_add_entities(
|
|
||||||
IntellifireSensor(coordinator=coordinator, description=description)
|
|
||||||
for description in INTELLIFIRE_SENSORS
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
INTELLIFIRE_SENSORS: tuple[IntellifireSensorEntityDescription, ...] = (
|
INTELLIFIRE_SENSORS: tuple[IntellifireSensorEntityDescription, ...] = (
|
||||||
@ -126,3 +86,26 @@ INTELLIFIRE_SENSORS: tuple[IntellifireSensorEntityDescription, ...] = (
|
|||||||
value_fn=_time_remaining_to_timestamp,
|
value_fn=_time_remaining_to_timestamp,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Define setup entry call."""
|
||||||
|
|
||||||
|
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
async_add_entities(
|
||||||
|
IntellifireSensor(coordinator=coordinator, description=description)
|
||||||
|
for description in INTELLIFIRE_SENSORS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class IntellifireSensor(IntellifireEntity, SensorEntity):
|
||||||
|
"""Extends IntellifireEntity with Sensor specific logic."""
|
||||||
|
|
||||||
|
entity_description: IntellifireSensorEntityDescription
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> int | str | datetime | None:
|
||||||
|
"""Return the state."""
|
||||||
|
return self.entity_description.value_fn(self.coordinator.api.data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user