diff --git a/homeassistant/components/plugwise/entity.py b/homeassistant/components/plugwise/entity.py index 349ee6d05e9..d57681ae504 100644 --- a/homeassistant/components/plugwise/entity.py +++ b/homeassistant/components/plugwise/entity.py @@ -1,6 +1,8 @@ """Generic Plugwise Entity Class.""" from __future__ import annotations +from typing import Any + from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -53,6 +55,11 @@ class PlugwiseEntity(CoordinatorEntity[PlugwiseData]): """Return if entity is available.""" return super().available and self._dev_id in self.coordinator.data.devices + @property + def device(self) -> dict[str, Any]: + """Return data for this device.""" + return self.coordinator.data.devices[self._dev_id] + async def async_added_to_hass(self) -> None: """Subscribe to updates.""" self._handle_coordinator_update() diff --git a/homeassistant/components/plugwise/switch.py b/homeassistant/components/plugwise/switch.py index 5365d5834a8..e7fb0b6f3db 100644 --- a/homeassistant/components/plugwise/switch.py +++ b/homeassistant/components/plugwise/switch.py @@ -5,10 +5,10 @@ from typing import Any from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN, LOGGER +from .const import DOMAIN from .coordinator import PlugwiseDataUpdateCoordinator from .entity import PlugwiseEntity from .util import plugwise_command @@ -51,14 +51,19 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity): super().__init__(coordinator, device_id) self.entity_description = description self._attr_unique_id = f"{device_id}-{description.key}" - self._attr_name = coordinator.data.devices[device_id].get("name") + self._attr_name = (f"{self.device.get('name', '')} {description.name}").lstrip() + + @property + def is_on(self) -> bool | None: + """Return True if entity is on.""" + return self.device["switches"].get(self.entity_description.key) @plugwise_command async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" await self.coordinator.api.set_switch_state( self._dev_id, - self.coordinator.data.devices[self._dev_id].get("members"), + self.device.get("members"), self.entity_description.key, "on", ) @@ -68,18 +73,7 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity): """Turn the device off.""" await self.coordinator.api.set_switch_state( self._dev_id, - self.coordinator.data.devices[self._dev_id].get("members"), + self.device.get("members"), self.entity_description.key, "off", ) - - @callback - def _handle_coordinator_update(self) -> None: - """Handle updated data from the coordinator.""" - if not (data := self.coordinator.data.devices.get(self._dev_id)): - LOGGER.error("Received no data for device %s", self._dev_id) - super()._handle_coordinator_update() - return - - self._attr_is_on = data["switches"].get(self.entity_description.key) - super()._handle_coordinator_update() diff --git a/tests/components/plugwise/test_switch.py b/tests/components/plugwise/test_switch.py index 2816b2dece6..4785a222f69 100644 --- a/tests/components/plugwise/test_switch.py +++ b/tests/components/plugwise/test_switch.py @@ -17,10 +17,10 @@ async def test_adam_climate_switch_entities(hass, mock_smile_adam): entry = await async_init_integration(hass, mock_smile_adam) assert entry.state is ConfigEntryState.LOADED - state = hass.states.get("switch.cv_pomp") + state = hass.states.get("switch.cv_pomp_relay") assert str(state.state) == "on" - state = hass.states.get("switch.fibaro_hc2") + state = hass.states.get("switch.fibaro_hc2_relay") assert str(state.state) == "on" @@ -34,7 +34,7 @@ async def test_adam_climate_switch_negative_testing(hass, mock_smile_adam): await hass.services.async_call( "switch", "turn_off", - {"entity_id": "switch.cv_pomp"}, + {"entity_id": "switch.cv_pomp_relay"}, blocking=True, ) @@ -47,7 +47,7 @@ async def test_adam_climate_switch_negative_testing(hass, mock_smile_adam): await hass.services.async_call( "switch", "turn_on", - {"entity_id": "switch.fibaro_hc2"}, + {"entity_id": "switch.fibaro_hc2_relay"}, blocking=True, ) @@ -65,7 +65,7 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam): await hass.services.async_call( "switch", "turn_off", - {"entity_id": "switch.cv_pomp"}, + {"entity_id": "switch.cv_pomp_relay"}, blocking=True, ) @@ -77,7 +77,7 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam): await hass.services.async_call( "switch", "toggle", - {"entity_id": "switch.fibaro_hc2"}, + {"entity_id": "switch.fibaro_hc2_relay"}, blocking=True, ) @@ -89,7 +89,7 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam): await hass.services.async_call( "switch", "turn_on", - {"entity_id": "switch.fibaro_hc2"}, + {"entity_id": "switch.fibaro_hc2_relay"}, blocking=True, ) @@ -104,10 +104,10 @@ async def test_stretch_switch_entities(hass, mock_stretch): entry = await async_init_integration(hass, mock_stretch) assert entry.state is ConfigEntryState.LOADED - state = hass.states.get("switch.koelkast_92c4a") + state = hass.states.get("switch.koelkast_92c4a_relay") assert str(state.state) == "on" - state = hass.states.get("switch.droger_52559") + state = hass.states.get("switch.droger_52559_relay") assert str(state.state) == "on" @@ -119,7 +119,7 @@ async def test_stretch_switch_changes(hass, mock_stretch): await hass.services.async_call( "switch", "turn_off", - {"entity_id": "switch.koelkast_92c4a"}, + {"entity_id": "switch.koelkast_92c4a_relay"}, blocking=True, ) assert mock_stretch.set_switch_state.call_count == 1 @@ -130,7 +130,7 @@ async def test_stretch_switch_changes(hass, mock_stretch): await hass.services.async_call( "switch", "toggle", - {"entity_id": "switch.droger_52559"}, + {"entity_id": "switch.droger_52559_relay"}, blocking=True, ) assert mock_stretch.set_switch_state.call_count == 2 @@ -141,7 +141,7 @@ async def test_stretch_switch_changes(hass, mock_stretch): await hass.services.async_call( "switch", "turn_on", - {"entity_id": "switch.droger_52559"}, + {"entity_id": "switch.droger_52559_relay"}, blocking=True, ) assert mock_stretch.set_switch_state.call_count == 3