10000 Improve smarla base entity (#145710) · home-assistant/core@d87fdf0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d87fdf0

Browse files
Improve smarla base entity (#145710)
1 parent 6f5d5d4 commit d87fdf0

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

homeassistant/components/smarla/entity.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
"""Common base for entities."""
22

3+
from dataclasses import dataclass
34
from typing import Any
45

56
from pysmarlaapi import Federwiege
6-
from pysmarlaapi.federwiege.classes import Property
77

88
from homeassistant.helpers.device_registry import DeviceInfo
9-
from homeassistant.helpers.entity import Entity
9+
from homeassistant.helpers.entity import Entity, EntityDescription
1010

1111
from .const import DEVICE_MODEL_NAME, DOMAIN, MANUFACTURER_NAME
1212

1313

14+
@dataclass(frozen=True, kw_only=True)
15+
class SmarlaEntityDescription(EntityDescription):
16+
"""Class describing Swing2Sleep Smarla entities."""
17+
18+
service: str
19+
property: str
20+
21+
1422
class SmarlaBaseEntity(Entity):
1523
"""Common Base Entity class for defining Smarla device."""
1624

25+
entity_description: SmarlaEntityDescription
26+
1727
_attr_should_poll = False
1828
_attr_has_entity_name = True
1929

20-
def __init__(self, federwiege: Federwiege, prop: Property) -> None:
30+
def __init__(self, federwiege: Federwiege, desc: SmarlaEntityDescription) -> None:
2131
"""Initialise the entity."""
22-
self._property = prop
32+
self.entity_description = desc
33+
self._property = federwiege.get_property(desc.service, desc.property)
34+
self._attr_unique_id = f"{federwiege.serial_number}-{desc.key}"
2335
self._attr_device_info = DeviceInfo(
2436
identifiers={(DOMAIN, federwiege.serial_number)},
2537
name=DEVICE_MODEL_NAME,

homeassistant/components/smarla/switch.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33
from dataclasses import dataclass
44
from typing import Any
55

6-
from pysmarlaapi import Federwiege
76
from pysmarlaapi.federwiege.classes import Property
87

98
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
109
from homeassistant.core import HomeAssistant
1110
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1211

1312
from . import FederwiegeConfigEntry
14-
from .entity import SmarlaBaseEntity
13+
from .entity import SmarlaBaseEntity, SmarlaEntityDescription
1514

1615

1716
@dataclass(frozen=True, kw_only=True)
18-
class SmarlaSwitchEntityDescription(SwitchEntityDescription):
17+
class SmarlaSwitchEntityDescription(SmarlaEntityDescription, SwitchEntityDescription):
1918
"""Class describing Swing2Sleep Smarla switch entity."""
2019

21-
service: str
22-
property: str
23-
2420

2521
SWITCHES: list[SmarlaSwitchEntityDescription] = [
2622
SmarlaSwitchEntityDescription(
@@ -55,17 +51,6 @@ class SmarlaSwitch(SmarlaBaseEntity, SwitchEntity):
5551

5652
_property: Property[bool]
5753

58-
def __init__(
59-
self,
60-
federwiege: Federwiege,
61-
desc: SmarlaSwitchEntityDescription,
62-
) -> None:
63-
"""Initialize a Smarla switch."""
64-
prop = federwiege.get_property(desc.service, desc.property)
65-
super().__init__(federwiege, prop)
66-
self.entity_description = desc
67-
self._attr_unique_id = f"{federwiege.serial_number}-{desc.key}"
68-
6954
@property
7055
def is_on(self) -> bool:
7156
"""Return the entity value to represent the entity state."""

0 commit comments

Comments
 (0)
0