Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 28e18ce

Browse files
thecodebdraco
andauthored
Fix Shelly Gen1 entity description restore (home-assistant#108052)
* Fix Shelly Gen1 entity description restore * Update tests/components/shelly/test_sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> --------- Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent 3bc20a0 commit 28e18ce

File tree

5 files changed

+24
-74
lines changed

5 files changed

+24
-74
lines changed

‎homeassistant/components/shelly/binary_sensor.py‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from homeassistant.const import STATE_ON, EntityCategory
1616
from homeassistant.core import HomeAssistant
1717
from homeassistant.helpers.entity_platform import AddEntitiesCallback
18-
from homeassistant.helpers.entity_registry import RegistryEntry
1918
from homeassistant.helpers.restore_state import RestoreEntity
2019

2120
from .const import CONF_SLEEP_PERIOD
@@ -210,16 +209,6 @@ class RestBinarySensorDescription(RestEntityDescription, BinarySensorEntityDescr
210209
}
211210

212211

213-
def _build_block_description(entry: RegistryEntry) -> BlockBinarySensorDescription:
214-
"""Build description when restoring block attribute entities."""
215-
return BlockBinarySensorDescription(
216-
key="",
217-
name="",
218-
icon=entry.original_icon,
219-
device_class=entry.original_device_class,
220-
)
221-
222-
223212
async def async_setup_entry(
224213
hass: HomeAssistant,
225214
config_entry: ConfigEntry,
@@ -248,7 +237,6 @@ async def async_setup_entry(
248237
async_add_entities,
249238
SENSORS,
250239
BlockSleepingBinarySensor,
251-
_build_block_description,
252240
)
253241
else:
254242
async_setup_entry_attribute_entities(
@@ -257,7 +245,6 @@ async def async_setup_entry(
257245
async_add_entities,
258246
SENSORS,
259247
BlockBinarySensor,
260-
_build_block_description,
261248
)
262249
async_setup_entry_rest(
263250
hass,

‎homeassistant/components/shelly/entity.py‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def async_setup_entry_attribute_entities(
3939
async_add_entities: AddEntitiesCallback,
4040
sensors: Mapping[tuple[str, str], BlockEntityDescription],
4141
sensor_class: Callable,
42-
description_class: Callable[[RegistryEntry], BlockEntityDescription],
4342
) -> None:
4443
"""Set up entities for attributes."""
4544
coordinator = get_entry_data(hass)[config_entry.entry_id].block
@@ -56,7 +55,6 @@ def async_setup_entry_attribute_entities(
5655
coordinator,
5756
sensors,
5857
sensor_class,
59-
description_class,
6058
)
6159

6260

@@ -113,7 +111,6 @@ def async_restore_block_attribute_entities(
113111
coordinator: ShellyBlockCoordinator,
114112
sensors: Mapping[tuple[str, str], BlockEntityDescription],
115113
sensor_class: Callable,
116-
description_class: Callable[[RegistryEntry], BlockEntityDescription],
117114
) -> None:
118115
"""Restore block attributes entities."""
119116
entities = []
@@ -128,11 +125,12 @@ def async_restore_block_attribute_entities(
128125
continue
129126

130127
attribute = entry.unique_id.split("-")[-1]
131-
description = description_class(entry)
128+
block_type = entry.unique_id.split("-")[-2].split("_")[0]
132129

133-
entities.append(
134-
sensor_class(coordinator, None, attribute, description, entry, sensors)
135-
)
130+
if description := sensors.get((block_type, attribute)):
131+
entities.append(
132+
sensor_class(coordinator, None, attribute, description, entry)
133+
)
136134

137135
if not entities:
138136
return
@@ -444,7 +442,7 @@ def available(self) -> bool:
444442
"""Available."""
445443
available = super().available
446444

447-
if not available or not self.entity_description.available:
445+
if not available or not self.entity_description.availableorself.blockisNone:
448446
return available
449447

450448
return self.entity_description.available(self.block)
@@ -559,10 +557,8 @@ def __init__(
559557
attribute: str,
560558
description: BlockEntityDescription,
561559
entry: RegistryEntry | None = None,
562-
sensors: Mapping[tuple[str, str], BlockEntityDescription] | None = None,
563560
) -> None:
564561
"""Initialize the sleeping sensor."""
565-
self.sensors = sensors
566562
self.last_state: State | None = None
567563
self.coordinator = coordinator
568564
self.attribute = attribute
@@ -587,11 +583,7 @@ def __init__(
587583
@callback
588584
def _update_callback(self) -> None:
589585
"""Handle device update."""
590-
if (
591-
self.block is not None
592-
or not self.coordinator.device.initialized
593-
or self.sensors is None
594-
):
586+
if self.block is not None or not self.coordinator.device.initialized:
595587
super()._update_callback()
596588
return
597589

@@ -607,13 +599,7 @@ def _update_callback(self) -> None:
607599
if sensor_id != entity_sensor:
608600
continue
609601

610-
description = self.sensors.get((block.type, sensor_id))
611-
if description is None:
612-
continue
613-
614602
self.block = block
615-
self.entity_description = description
616-
617603
LOGGER.debug("Entity %s attached to block", self.name)
618604
super()._update_callback()
619605
return

‎homeassistant/components/shelly/number.py‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Number for Shelly."""
22
from __future__ import annotations
33

4-
from collections.abc import Mapping
54
from dataclasses import dataclass
65
from typing import Any, Final, cast
76

@@ -56,22 +55,6 @@ class BlockNumberDescription(BlockEntityDescription, NumberEntityDescription):
5655
}
5756

5857

59-
def _build_block_description(entry: RegistryEntry) -> BlockNumberDescription:
60-
"""Build description when restoring block attribute entities."""
61-
assert entry.capabilities
62-
return BlockNumberDescription(
63-
key="",
64-
name="",
65-
icon=entry.original_icon,
66-
native_unit_of_measurement=entry.unit_of_measurement,
67-
device_class=entry.original_device_class,
68-
native_min_value=cast(float, entry.capabilities.get("min")),
69-
native_max_value=cast(float, entry.capabilities.get("max")),
70-
native_step=cast(float, entry.capabilities.get("step")),
71-
mode=cast(NumberMode, entry.capabilities.get("mode")),
72-
)
73-
74-
7558
async def async_setup_entry(
7659
hass: HomeAssistant,
7760
config_entry: ConfigEntry,
@@ -85,7 +68,6 @@ async def async_setup_entry(
8568
async_add_entities,
8669
NUMBERS,
8770
BlockSleepingNumber,
88-
_build_block_description,
8971
)
9072

9173

@@ -101,11 +83,10 @@ def __init__(
10183
attribute: str,
10284
description: BlockNumberDescription,
10385
entry: RegistryEntry | None = None,
104-
sensors: Mapping[tuple[str, str], BlockNumberDescription] | None = None,
10586
) -> None:
10687
"""Initialize the sleeping sensor."""
10788
self.restored_data: NumberExtraStoredData | None = None
108-
super().__init__(coordinator, block, attribute, description, entry, sensors)
89+
super().__init__(coordinator, block, attribute, description, entry)
10990

11091
async def async_added_to_hass(self) -> None:
11192
"""Handle entity which will be added."""

‎homeassistant/components/shelly/sensor.py‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Sensor for Shelly."""
22
from __future__ import annotations
33

4-
from collections.abc import Mapping
54
from dataclasses import dataclass
65
from typing import Final, cast
76

@@ -36,7 +35,6 @@
3635
from homeassistant.helpers.entity_platform import AddEntitiesCallback
3736
from homeassistant.helpers.entity_registry import RegistryEntry
3837
from homeassistant.helpers.typing import StateType
39-
from homeassistant.util.enum import try_parse_enum
4038

4139
from .const import CONF_SLEEP_PERIOD, SHAIR_MAX_WORK_HOURS
4240
from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator
@@ -963,17 +961,6 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription):
963961
}
964962

965963

966-
def _build_block_description(entry: RegistryEntry) -> BlockSensorDescription:
967-
"""Build description when restoring block attribute entities."""
968-
return BlockSensorDescription(
969-
key="",
970-
name="",
971-
icon=entry.original_icon,
972-
native_unit_of_measurement=entry.unit_of_measurement,
973-
device_class=try_parse_enum(SensorDeviceClass, entry.original_device_class),
974-
)
975-
976-
977964
async def async_setup_entry(
978965
hass: HomeAssistant,
979966
config_entry: ConfigEntry,
@@ -1002,7 +989,6 @@ async def async_setup_entry(
1002989
async_add_entities,
1003990
SENSORS,
1004991
BlockSleepingSensor,
1005-
_build_block_description,
1006992
)
1007993
else:
1008994
async_setup_entry_attribute_entities(
@@ -1011,7 +997,6 @@ async def async_setup_entry(
1011997
async_add_entities,
1012998
SENSORS,
1013999
BlockSensor,
1014-
_build_block_description,
10151000
)
10161001
async_setup_entry_rest(
10171002
hass, config_entry, async_add_entities, REST_SENSORS, RestSensor
@@ -1075,10 +1060,9 @@ def __init__(
10751060
attribute: str,
10761061
description: BlockSensorDescription,
10771062
entry: RegistryEntry | None = None,
1078-
sensors: Mapping[tuple[str, str], BlockSensorDescription] | None = None,
10791063
) -> None:
10801064
"""Initialize the sleeping sensor."""
1081-
super().__init__(coordinator, block, attribute, description, entry, sensors)
1065+
super().__init__(coordinator, block, attribute, description, entry)
10821066
self.restored_data: SensorExtraStoredData | None = None
10831067

10841068
async def async_added_to_hass(self) -> None:

‎tests/components/shelly/test_sensor.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
DOMAIN as HA_DOMAIN,
77
SERVICE_UPDATE_ENTITY,
88
)
9-
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
9+
from homeassistant.components.sensor import (
10+
ATTR_STATE_CLASS,
11+
DOMAIN as SENSOR_DOMAIN,
12+
SensorDeviceClass,
13+
SensorStateClass,
14+
)
1015
from homeassistant.components.shelly.const import DOMAIN
1116
from homeassistant.const import (
17+
ATTR_DEVICE_CLASS,
1218
ATTR_ENTITY_ID,
1319
ATTR_UNIT_OF_MEASUREMENT,
1420
PERCENTAGE,
@@ -153,7 +159,11 @@ async def test_block_restored_sleeping_sensor(
153159
await hass.config_entries.async_setup(entry.entry_id)
154160
await hass.async_block_till_done()
155161

156-
assert hass.states.get(entity_id).state == "20.4"
162+
state = hass.states.get(entity_id)
163+
assert state
164+
assert state.state == "20.4"
165+
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT
166+
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE
157167

158168
# Make device online
159169
monkeypatch.setattr(mock_block_device, "initialized", True)
@@ -237,7 +247,9 @@ async def test_block_not_matched_restored_sleeping_sensor(
237247
assert hass.states.get(entity_id).state == "20.4"
238248

239249
# Make device online
240-
monkeypatch.setattr(mock_block_device.blocks[SENSOR_BLOCK_ID], "type", "other_type")
250+
monkeypatch.setattr(
251+
mock_block_device.blocks[SENSOR_BLOCK_ID], "description", "other_desc"
252+
)
241253
monkeypatch.setattr(mock_block_device, "initialized", True)
242254
mock_block_device.mock_update()
243255
await hass.async_block_till_done()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /