From 0fdf04391b1d115ea1e5dec07459b6b16b5e76b2 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Mon, 2 Oct 2023 11:40:06 +0000 Subject: [PATCH] Use class attrs and shorthand attrs for Shelly (#101249) --- homeassistant/components/shelly/climate.py | 18 +++++------------- homeassistant/components/shelly/cover.py | 12 ++++++------ homeassistant/components/shelly/entity.py | 4 ---- homeassistant/components/shelly/event.py | 2 -- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/shelly/climate.py b/homeassistant/components/shelly/climate.py index a9712e62d25..35c18511860 100644 --- a/homeassistant/components/shelly/climate.py +++ b/homeassistant/components/shelly/climate.py @@ -148,6 +148,7 @@ class BlockSleepingClimate( self.last_state_attributes: Mapping[str, Any] self._preset_modes: list[str] = [] self._last_target_temp = SHTRV_01_TEMPERATURE_SETTINGS["default"] + self._attr_name = coordinator.name if self.block is not None and self.device_block is not None: self._unique_id = f"{self.coordinator.mac}-{self.block.description}" @@ -160,6 +161,9 @@ class BlockSleepingClimate( ] elif entry is not None: self._unique_id = entry.unique_id + self._attr_device_info = DeviceInfo( + connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}, + ) self._channel = cast(int, self._unique_id.split("_")[1]) @@ -173,11 +177,6 @@ class BlockSleepingClimate( """Set unique id of entity.""" return self._unique_id - @property - def name(self) -> str: - """Name of entity.""" - return self.coordinator.name - @property def target_temperature(self) -> float | None: """Set target temperature.""" @@ -256,13 +255,6 @@ class BlockSleepingClimate( """Preset available modes.""" return self._preset_modes - @property - def device_info(self) -> DeviceInfo: - """Device info.""" - return DeviceInfo( - connections={(CONNECTION_NETWORK_MAC, self.coordinator.mac)}, - ) - def _check_is_off(self) -> bool: """Return if valve is off or on.""" return bool( @@ -354,7 +346,7 @@ class BlockSleepingClimate( severity=ir.IssueSeverity.ERROR, translation_key="device_not_calibrated", translation_placeholders={ - "device_name": self.name, + "device_name": self.coordinator.name, "ip_address": self.coordinator.device.ip_address, }, ) diff --git a/homeassistant/components/shelly/cover.py b/homeassistant/components/shelly/cover.py index f2020597277..3d3e5be5b91 100644 --- a/homeassistant/components/shelly/cover.py +++ b/homeassistant/components/shelly/cover.py @@ -70,14 +70,14 @@ class BlockShellyCover(ShellyBlockEntity, CoverEntity): """Entity that controls a cover on block based Shelly devices.""" _attr_device_class = CoverDeviceClass.SHUTTER + _attr_supported_features = ( + CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP + ) def __init__(self, coordinator: ShellyBlockCoordinator, block: Block) -> None: """Initialize block cover.""" super().__init__(coordinator, block) self.control_result: dict[str, Any] | None = None - self._attr_supported_features = ( - CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP - ) if self.coordinator.device.settings["rollers"][0]["positioning"]: self._attr_supported_features |= CoverEntityFeature.SET_POSITION @@ -146,14 +146,14 @@ class RpcShellyCover(ShellyRpcEntity, CoverEntity): """Entity that controls a cover on RPC based Shelly devices.""" _attr_device_class = CoverDeviceClass.SHUTTER + _attr_supported_features = ( + CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP + ) def __init__(self, coordinator: ShellyRpcCoordinator, id_: int) -> None: """Initialize rpc cover.""" super().__init__(coordinator, f"cover:{id_}") self._id = id_ - self._attr_supported_features = ( - CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP - ) if self.status["pos_control"]: self._attr_supported_features |= CoverEntityFeature.SET_POSITION diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 5afa5f8b727..92100eaddaf 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -326,7 +326,6 @@ class ShellyBlockEntity(CoordinatorEntity[ShellyBlockCoordinator]): super().__init__(coordinator) self.block = block self._attr_name = get_block_entity_name(coordinator.device, block) - self._attr_should_poll = False self._attr_device_info = DeviceInfo( connections={(CONNECTION_NETWORK_MAC, coordinator.mac)} ) @@ -364,7 +363,6 @@ class ShellyRpcEntity(CoordinatorEntity[ShellyRpcCoordinator]): """Initialize Shelly entity.""" super().__init__(coordinator) self.key = key - self._attr_should_poll = False self._attr_device_info = { "connections": {(CONNECTION_NETWORK_MAC, coordinator.mac)} } @@ -571,7 +569,6 @@ class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity): self.block: Block | None = block # type: ignore[assignment] self.entity_description = description - self._attr_should_poll = False self._attr_device_info = DeviceInfo( connections={(CONNECTION_NETWORK_MAC, coordinator.mac)} ) @@ -643,7 +640,6 @@ class ShellySleepingRpcAttributeEntity(ShellyRpcAttributeEntity): self.attribute = attribute self.entity_description = description - self._attr_should_poll = False self._attr_device_info = DeviceInfo( connections={(CONNECTION_NETWORK_MAC, coordinator.mac)} ) diff --git a/homeassistant/components/shelly/event.py b/homeassistant/components/shelly/event.py index 1b0fedd5cda..1b5cf911e85 100644 --- a/homeassistant/components/shelly/event.py +++ b/homeassistant/components/shelly/event.py @@ -122,7 +122,6 @@ async def async_setup_entry( class ShellyBlockEvent(ShellyBlockEntity, EventEntity): """Represent Block event entity.""" - _attr_should_poll = False entity_description: ShellyBlockEventDescription def __init__( @@ -160,7 +159,6 @@ class ShellyBlockEvent(ShellyBlockEntity, EventEntity): class ShellyRpcEvent(CoordinatorEntity[ShellyRpcCoordinator], EventEntity): """Represent RPC event entity.""" - _attr_should_poll = False entity_description: ShellyRpcEventDescription def __init__(