mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Improve smarla base entity (#145710)
This commit is contained in:
parent
6f5d5d4cdb
commit
d87fdf028b
@ -1,25 +1,37 @@
|
|||||||
"""Common base for entities."""
|
"""Common base for entities."""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pysmarlaapi import Federwiege
|
from pysmarlaapi import Federwiege
|
||||||
from pysmarlaapi.federwiege.classes import Property
|
|
||||||
|
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||||
|
|
||||||
from .const import DEVICE_MODEL_NAME, DOMAIN, MANUFACTURER_NAME
|
from .const import DEVICE_MODEL_NAME, DOMAIN, MANUFACTURER_NAME
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class SmarlaEntityDescription(EntityDescription):
|
||||||
|
"""Class describing Swing2Sleep Smarla entities."""
|
||||||
|
|
||||||
|
service: str
|
||||||
|
property: str
|
||||||
|
|
||||||
|
|
||||||
class SmarlaBaseEntity(Entity):
|
class SmarlaBaseEntity(Entity):
|
||||||
"""Common Base Entity class for defining Smarla device."""
|
"""Common Base Entity class for defining Smarla device."""
|
||||||
|
|
||||||
|
entity_description: SmarlaEntityDescription
|
||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(self, federwiege: Federwiege, prop: Property) -> None:
|
def __init__(self, federwiege: Federwiege, desc: SmarlaEntityDescription) -> None:
|
||||||
"""Initialise the entity."""
|
"""Initialise the entity."""
|
||||||
self._property = prop
|
self.entity_description = desc
|
||||||
|
self._property = federwiege.get_property(desc.service, desc.property)
|
||||||
|
self._attr_unique_id = f"{federwiege.serial_number}-{desc.key}"
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, federwiege.serial_number)},
|
identifiers={(DOMAIN, federwiege.serial_number)},
|
||||||
name=DEVICE_MODEL_NAME,
|
name=DEVICE_MODEL_NAME,
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pysmarlaapi import Federwiege
|
|
||||||
from pysmarlaapi.federwiege.classes import Property
|
from pysmarlaapi.federwiege.classes import Property
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
@ -11,16 +10,13 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from . import FederwiegeConfigEntry
|
from . import FederwiegeConfigEntry
|
||||||
from .entity import SmarlaBaseEntity
|
from .entity import SmarlaBaseEntity, SmarlaEntityDescription
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class SmarlaSwitchEntityDescription(SwitchEntityDescription):
|
class SmarlaSwitchEntityDescription(SmarlaEntityDescription, SwitchEntityDescription):
|
||||||
"""Class describing Swing2Sleep Smarla switch entity."""
|
"""Class describing Swing2Sleep Smarla switch entity."""
|
||||||
|
|
||||||
service: str
|
|
||||||
property: str
|
|
||||||
|
|
||||||
|
|
||||||
SWITCHES: list[SmarlaSwitchEntityDescription] = [
|
SWITCHES: list[SmarlaSwitchEntityDescription] = [
|
||||||
SmarlaSwitchEntityDescription(
|
SmarlaSwitchEntityDescription(
|
||||||
@ -55,17 +51,6 @@ class SmarlaSwitch(SmarlaBaseEntity, SwitchEntity):
|
|||||||
|
|
||||||
_property: Property[bool]
|
_property: Property[bool]
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
federwiege: Federwiege,
|
|
||||||
desc: SmarlaSwitchEntityDescription,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize a Smarla switch."""
|
|
||||||
prop = federwiege.get_property(desc.service, desc.property)
|
|
||||||
super().__init__(federwiege, prop)
|
|
||||||
self.entity_description = desc
|
|
||||||
self._attr_unique_id = f"{federwiege.serial_number}-{desc.key}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return the entity value to represent the entity state."""
|
"""Return the entity value to represent the entity state."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user