More cleanup in Plugwise switch (#66254)

This commit is contained in:
Franck Nijhof 2022-02-10 10:17:37 +01:00 committed by GitHub
parent ea325ef027
commit 8760cb035a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 28 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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