Skip to content

Commit b6bd92e

Browse files
authored
Shelly entity device info code quality (home-assistant#149477)
1 parent 7976729 commit b6bd92e

File tree

5 files changed

+52
-102
lines changed

5 files changed

+52
-102
lines changed

homeassistant/components/shelly/button.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@
2525

2626
from .const import DOMAIN, LOGGER, SHELLY_GAS_MODELS
2727
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
28-
from .utils import (
29-
get_block_device_info,
30-
get_blu_trv_device_info,
31-
get_device_entry_gen,
32-
get_rpc_device_info,
33-
get_rpc_key_ids,
34-
)
28+
from .entity import get_entity_block_device_info, get_entity_rpc_device_info
29+
from .utils import get_blu_trv_device_info, get_device_entry_gen, get_rpc_key_ids
3530

3631
PARALLEL_UPDATES = 0
3732

@@ -233,23 +228,9 @@ def __init__(
233228

234229
self._attr_unique_id = f"{coordinator.mac}_{description.key}"
235230
if isinstance(coordinator, ShellyBlockCoordinator):
236-
self._attr_device_info = get_block_device_info(
237-
coordinator.device,
238-
coordinator.mac,
239-
coordinator.configuration_url,
240-
coordinator.model,
241-
coordinator.model_name,
242-
suggested_area=coordinator.suggested_area,
243-
)
231+
self._attr_device_info = get_entity_block_device_info(coordinator)
244232
else:
245-
self._attr_device_info = get_rpc_device_info(
246-
coordinator.device,
247-
coordinator.mac,
248-
coordinator.configuration_url,
249-
coordinator.model,
250-
coordinator.model_name,
251-
suggested_area=coordinator.suggested_area,
252-
)
233+
self._attr_device_info = get_entity_rpc_device_info(coordinator)
253234

254235
async def _press_method(self) -> None:
255236
"""Press method."""

homeassistant/components/shelly/climate.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
SHTRV_01_TEMPERATURE_SETTINGS,
3939
)
4040
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
41-
from .entity import ShellyRpcEntity, rpc_call
41+
from .entity import ShellyRpcEntity, get_entity_block_device_info, rpc_call
4242
from .utils import (
4343
async_remove_shelly_entity,
44-
get_block_device_info,
4544
get_block_entity_name,
4645
get_blu_trv_device_info,
4746
get_device_entry_gen,
@@ -210,15 +209,7 @@ def __init__(
210209
]
211210
elif entry is not None:
212211
self._unique_id = entry.unique_id
213-
self._attr_device_info = get_block_device_info(
214-
coordinator.device,
215-
coordinator.mac,
216-
coordinator.configuration_url,
217-
coordinator.model,
218-
coordinator.model_name,
219-
sensor_block,
220-
suggested_area=coordinator.suggested_area,
221-
)
212+
self._attr_device_info = get_entity_block_device_info(coordinator, sensor_block)
222213
self._attr_name = get_block_entity_name(
223214
self.coordinator.device, sensor_block, None
224215
)

homeassistant/components/shelly/entity.py

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from homeassistant.core import HomeAssistant, State, callback
1414
from homeassistant.exceptions import HomeAssistantError
1515
from homeassistant.helpers import entity_registry as er
16+
from homeassistant.helpers.device_registry import DeviceInfo
1617
from homeassistant.helpers.entity import Entity, EntityDescription
1718
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1819
from homeassistant.helpers.entity_registry import RegistryEntry
@@ -368,15 +369,7 @@ def __init__(self, coordinator: ShellyBlockCoordinator, block: Block) -> None:
368369
super().__init__(coordinator)
369370
self.block = block
370371
self._attr_name = get_block_entity_name(coordinator.device, block)
371-
self._attr_device_info = get_block_device_info(
372-
coordinator.device,
373-
coordinator.mac,
374-
coordinator.configuration_url,
375-
coordinator.model,
376-
coordinator.model_name,
377-
block,
378-
suggested_area=coordinator.suggested_area,
379-
)
372+
self._attr_device_info = get_entity_block_device_info(coordinator, block)
380373
self._attr_unique_id = f"{coordinator.mac}-{block.description}"
381374

382375
# pylint: disable-next=hass-missing-super-call
@@ -417,15 +410,7 @@ def __init__(self, coordinator: ShellyRpcCoordinator, key: str) -> None:
417410
"""Initialize Shelly entity."""
418411
super().__init__(coordinator)
419412
self.key = key
420-
self._attr_device_info = get_rpc_device_info(
421-
coordinator.device,
422-
coordinator.mac,
423-
coordinator.configuration_url,
424-
coordinator.model,
425-
coordinator.model_name,
426-
key,
427-
suggested_area=coordinator.suggested_area,
428-
)
413+
self._attr_device_info = get_entity_rpc_device_info(coordinator, key)
429414
self._attr_unique_id = f"{coordinator.mac}-{key}"
430415
self._attr_name = get_rpc_entity_name(coordinator.device, key)
431416

@@ -539,14 +524,7 @@ def __init__(
539524
coordinator.device, None, description.name
540525
)
541526
self._attr_unique_id = f"{coordinator.mac}-{attribute}"
542-
self._attr_device_info = get_block_device_info(
543-
coordinator.device,
544-
coordinator.mac,
545-
coordinator.configuration_url,
546-
coordinator.model,
547-
coordinator.model_name,
548-
suggested_area=coordinator.suggested_area,
549-
)
527+
self._attr_device_info = get_entity_block_device_info(coordinator)
550528
self._last_value = None
551529

552530
@property
@@ -653,15 +631,7 @@ def __init__(
653631
self.block: Block | None = block # type: ignore[assignment]
654632
self.entity_description = description
655633

656-
self._attr_device_info = get_block_device_info(
657-
coordinator.device,
658-
coordinator.mac,
659-
coordinator.configuration_url,
660-
coordinator.model,
661-
coordinator.model_name,
662-
block,
663-
suggested_area=coordinator.suggested_area,
664-
)
634+
self._attr_device_info = get_entity_block_device_info(coordinator, block)
665635

666636
if block is not None:
667637
self._attr_unique_id = (
@@ -726,18 +696,8 @@ def __init__(
726696
self.attribute = attribute
727697
self.entity_description = description
728698

729-
self._attr_device_info = get_rpc_device_info(
730-
coordinator.device,
731-
coordinator.mac,
732-
coordinator.configuration_url,
733-
coordinator.model,
734-
coordinator.model_name,
735-
key,
736-
suggested_area=coordinator.suggested_area,
737-
)
738-
self._attr_unique_id = self._attr_unique_id = (
739-
f"{coordinator.mac}-{key}-{attribute}"
740-
)
699+
self._attr_device_info = get_entity_rpc_device_info(coordinator, key)
700+
self._attr_unique_id = f"{coordinator.mac}-{key}-{attribute}"
741701
self._last_value = None
742702

743703
if coordinator.device.initialized:
@@ -763,3 +723,37 @@ def get_entity_class(
763723
return description.entity_class
764724

765725
return sensor_class
726+
727+
728+
def get_entity_block_device_info(
729+
coordinator: ShellyBlockCoordinator,
730+
block: Block | None = None,
731+
) -> DeviceInfo:
732+
"""Get device info for block entities."""
733+
return get_block_device_info(
734+
coordinator.device,
735+
coordinator.mac,
736+
coordinator.configuration_url,
737+
coordinator.model,
738+
coordinator.model_name,
739+
block,
740+
suggested_area=coordinator.suggested_area,
741+
)
742+
743+
744+
def get_entity_rpc_device_info(
745+
coordinator: ShellyRpcCoordinator,
746+
key: str | None = None,
747+
emeter_phase: str | None = None,
748+
) -> DeviceInfo:
749+
"""Get device info for RPC entities."""
750+
return get_rpc_device_info(
751+
coordinator.device,
752+
coordinator.mac,
753+
coordinator.configuration_url,
754+
coordinator.model,
755+
coordinator.model_name,
756+
key,
757+
emeter_phase=emeter_phase,
758+
suggested_area=coordinator.suggested_area,
759+
)

homeassistant/components/shelly/event.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
SHIX3_1_INPUTS_EVENTS_TYPES,
2727
)
2828
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
29-
from .entity import ShellyBlockEntity
29+
from .entity import ShellyBlockEntity, get_entity_rpc_device_info
3030
from .utils import (
3131
async_remove_orphaned_entities,
3232
async_remove_shelly_entity,
3333
get_device_entry_gen,
34-
get_rpc_device_info,
3534
get_rpc_entity_name,
3635
get_rpc_key_instances,
3736
is_block_momentary_input,
@@ -206,15 +205,7 @@ def __init__(
206205
"""Initialize Shelly entity."""
207206
super().__init__(coordinator)
208207
self.event_id = int(key.split(":")[-1])
209-
self._attr_device_info = get_rpc_device_info(
210-
coordinator.device,
211-
coordinator.mac,
212-
coordinator.configuration_url,
213-
coordinator.model,
214-
coordinator.model_name,
215-
key,
216-
suggested_area=coordinator.suggested_area,
217-
)
208+
self._attr_device_info = get_entity_rpc_device_info(coordinator, key)
218209
self._attr_unique_id = f"{coordinator.mac}-{key}"
219210
self._attr_name = get_rpc_entity_name(coordinator.device, key)
220211
self.entity_description = description

homeassistant/components/shelly/sensor.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252
async_setup_entry_attribute_entities,
5353
async_setup_entry_rest,
5454
async_setup_entry_rpc,
55+
get_entity_rpc_device_info,
5556
)
5657
from .utils import (
5758
async_remove_orphaned_entities,
5859
get_blu_trv_device_info,
5960
get_device_entry_gen,
6061
get_device_uptime,
61-
get_rpc_device_info,
6262
get_shelly_air_lamp_life,
6363
get_virtual_component_ids,
6464
is_rpc_wifi_stations_disabled,
@@ -138,15 +138,8 @@ def __init__(
138138
"""Initialize select."""
139139
super().__init__(coordinator, key, attribute, description)
140140

141-
self._attr_device_info = get_rpc_device_info(
142-
coordinator.device,
143-
coordinator.mac,
144-
coordinator.configuration_url,
145-
coordinator.model,
146-
coordinator.model_name,
147-
key,
148-
emeter_phase=description.emeter_phase,
149-
suggested_area=coordinator.suggested_area,
141+
self._attr_device_info = get_entity_rpc_device_info(
142+
coordinator, key, emeter_phase=description.emeter_phase
150143
)
151144

152145

0 commit comments

Comments
 (0)