Skip to content

Commit fd1961e

Browse files
committedFeb 25, 2024·
entity: add more annotations
1 parent 857146f commit fd1961e

File tree

2 files changed

+68
-61
lines changed

2 files changed

+68
-61
lines changed
 

‎custom_components/myheat/binary_sensor.py

+23-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from homeassistant.core import HomeAssistant
99
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1010

11-
from .const import CONF_NAME, DEFAULT_NAME, DOMAIN
11+
from .const import DOMAIN
1212
from .entity import MhEngEntity, MhEntity, MhEnvEntity, MhHeaterEntity
1313

1414
_logger = logging.getLogger(__package__)
@@ -22,38 +22,34 @@ async def async_setup_entry(
2222
"""Setup sensor platform."""
2323
coordinator = hass.data[DOMAIN][entry.entry_id]
2424

25-
_logger.info(f"Setting up heater entries: {coordinator.data}")
26-
27-
async_add_entities(
28-
[
25+
entities = chain(
26+
(
2927
MhSeverityBinarySensor(coordinator, entry),
3028
MhDataActualBinarySensor(coordinator, entry),
31-
]
32-
+ list(
33-
chain.from_iterable(
34-
[
35-
MhHeaterDisabledBinarySensor(coordinator, entry, heater),
36-
MhHeaterBurnerWaterBinarySensor(coordinator, entry, heater),
37-
MhHeaterBurnerHeatingBinarySensor(coordinator, entry, heater),
38-
]
39-
for heater in coordinator.data.get("heaters", [])
40-
)
41-
)
42-
+ list(
29+
),
30+
chain.from_iterable(
31+
[
32+
MhHeaterDisabledBinarySensor(coordinator, entry, heater),
33+
MhHeaterBurnerWaterBinarySensor(coordinator, entry, heater),
34+
MhHeaterBurnerHeatingBinarySensor(coordinator, entry, heater),
35+
]
36+
for heater in coordinator.data.get("heaters", [])
37+
),
38+
(
4339
MhEnvSeverityBinarySensor(coordinator, entry, env)
4440
for env in coordinator.data.get("envs", [])
45-
)
46-
+ list(
47-
chain.from_iterable(
48-
[
49-
MhEngTurnedOnBinarySensor(coordinator, entry, eng),
50-
MhEngSeverityBinarySensor(coordinator, entry, eng),
51-
]
52-
for eng in coordinator.data.get("engs", [])
53-
)
54-
)
41+
),
42+
chain.from_iterable(
43+
[
44+
MhEngTurnedOnBinarySensor(coordinator, entry, eng),
45+
MhEngSeverityBinarySensor(coordinator, entry, eng),
46+
]
47+
for eng in coordinator.data.get("engs", [])
48+
),
5549
)
5650

51+
async_add_entities(entities)
52+
5753

5854
class MhDataActualBinarySensor(MhEntity, BinarySensorEntity):
5955
"""myheat Data Actual (connected) Binary Sensor class."""

‎custom_components/myheat/entity.py

+45-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import logging
44

5-
from homeassistant.helpers.update_coordinator import CoordinatorEntity
5+
from homeassistant.config_entries import ConfigEntry
6+
from homeassistant.helpers.update_coordinator import (
7+
CoordinatorEntity,
8+
DataUpdateCoordinator,
9+
)
610

711
from .const import (
812
ATTRIBUTION,
@@ -18,13 +22,16 @@
1822

1923

2024
class MhEntity(CoordinatorEntity):
21-
def __init__(self, coordinator, config_entry):
25+
def __init__(
26+
self,
27+
coordinator: DataUpdateCoordinator,
28+
config_entry: ConfigEntry,
29+
):
2230
super().__init__(coordinator)
2331
self.config_entry = config_entry
2432

2533
@property
26-
def unique_id(self):
27-
"""Return a unique ID to use for this entity."""
34+
def unique_id(self) -> str:
2835
return self.config_entry.entry_id
2936

3037
@property
@@ -42,24 +49,23 @@ def device_info(self) -> dict:
4249
return info
4350

4451
@property
45-
def _mh_dev_name_suffix(self):
52+
def _mh_dev_name_suffix(self) -> str:
4653
return ""
4754

4855
@property
49-
def _mh_identifiers(self):
56+
def _mh_identifiers(self) -> tuple:
5057
return (DOMAIN, self.config_entry.entry_id)
5158

5259
@property
53-
def _mh_via_device(self):
60+
def _mh_via_device(self) -> tuple:
5461
return (DOMAIN, self.config_entry.entry_id)
5562

5663
@property
5764
def _mh_name(self) -> str:
5865
return self.config_entry.data.get(CONF_NAME, DEFAULT_NAME)
5966

6067
@property
61-
def device_state_attributes(self):
62-
"""Return the state attributes."""
68+
def device_state_attributes(self) -> dict:
6369
return {
6470
"attribution": ATTRIBUTION,
6571
"id": str(self.config_entry.data.get(CONF_DEVICE_ID)),
@@ -74,27 +80,30 @@ class MhHeaterEntity(MhEntity):
7480
heater_name: str = ""
7581
heater_id: int = 0
7682

77-
def __init__(self, coordinator, config_entry, heater: dict):
83+
def __init__(
84+
self,
85+
coordinator: DataUpdateCoordinator,
86+
config_entry: ConfigEntry,
87+
heater: dict,
88+
):
7889
super().__init__(coordinator, config_entry)
7990
self.heater_name = heater["name"]
8091
self.heater_id = heater["id"]
8192

8293
@property
83-
def name(self):
84-
"""Return the name of the sensor."""
94+
def name(self) -> str:
8595
return f"{self._mh_name} {self.heater_name}{' '+self._key if self._key else ''}"
8696

8797
@property
88-
def unique_id(self):
89-
"""Return a unique ID to use for this entity."""
98+
def unique_id(self) -> str:
9099
return f"{super().unique_id}htr{self.heater_id}{self._key if self._key else ''}"
91100

92101
@property
93-
def _mh_dev_name_suffix(self):
102+
def _mh_dev_name_suffix(self) -> str:
94103
return f" {self.heater_name}"
95104

96105
@property
97-
def _mh_identifiers(self):
106+
def _mh_identifiers(self) -> tuple:
98107
return (DOMAIN, f"{super().unique_id}htr{self.heater_id}")
99108

100109
def get_heater(self) -> dict:
@@ -117,32 +126,33 @@ class MhEnvEntity(MhEntity):
117126
env_name: str = ""
118127
env_id: int = 0
119128

120-
def __init__(self, coordinator, config_entry, env: dict):
129+
def __init__(
130+
self,
131+
coordinator: DataUpdateCoordinator,
132+
config_entry: ConfigEntry,
133+
env: dict,
134+
):
121135
super().__init__(coordinator, config_entry)
122136
self.env_name = env["name"]
123137
self.env_id = env["id"]
124138

125139
@property
126-
def name(self):
127-
"""Return the name of the sensor."""
140+
def name(self) -> str:
128141
return f"{self._mh_name} {self.env_name}{' '+self._key if self._key else ''}"
129142

130143
@property
131-
def unique_id(self):
132-
"""Return a unique ID to use for this entity."""
144+
def unique_id(self) -> str:
133145
return f"{super().unique_id}env{self.env_id}{self._key if self._key else ''}"
134146

135147
@property
136-
def _mh_dev_name_suffix(self):
148+
def _mh_dev_name_suffix(self) -> str:
137149
return f" {self.env_name}"
138150

139151
@property
140-
def _mh_identifiers(self):
152+
def _mh_identifiers(self) -> tuple:
141153
return (DOMAIN, f"{super().unique_id}env{self.env_id}")
142154

143-
def get_env(
144-
self,
145-
) -> dict:
155+
def get_env(self) -> dict:
146156
"""Return env state data"""
147157
if not self.coordinator.data.get("dataActual", False):
148158
_logger.warninig("data not actual! %s", self.coordinator.data)
@@ -162,32 +172,33 @@ class MhEngEntity(MhEntity):
162172
eng_name: str = ""
163173
eng_id: int = 0
164174

165-
def __init__(self, coordinator, config_entry, eng: dict):
175+
def __init__(
176+
self,
177+
coordinator: DataUpdateCoordinator,
178+
config_entry: ConfigEntry,
179+
eng: dict,
180+
):
166181
super().__init__(coordinator, config_entry)
167182
self.eng_name = eng["name"]
168183
self.eng_id = eng["id"]
169184

170185
@property
171186
def name(self):
172-
"""Return the name of the sensor."""
173187
return f"{self._mh_name} {self.eng_name}{' '+self._key if self._key else ''}"
174188

175189
@property
176190
def unique_id(self):
177-
"""Return a unique ID to use for this entity."""
178191
return f"{super().unique_id}eng{self.eng_id}{self._key if self._key else ''}"
179192

180193
@property
181-
def _mh_dev_name_suffix(self):
194+
def _mh_dev_name_suffix(self) -> str:
182195
return f" {self.eng_name}"
183196

184197
@property
185-
def _mh_identifiers(self):
198+
def _mh_identifiers(self) -> tuple:
186199
return (DOMAIN, f"{super().unique_id}eng{self.eng_id}")
187200

188-
def get_eng(
189-
self,
190-
) -> dict:
201+
def get_eng(self) -> dict:
191202
"""Return eng state data"""
192203
if not self.coordinator.data.get("dataActual", False):
193204
_logger.warninig("data not actual! %s", self.coordinator.data)

0 commit comments

Comments
 (0)
Please sign in to comment.