mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
More cleanup in Plugwise switch (#66254)
This commit is contained in:
parent
ea325ef027
commit
8760cb035a
@ -1,6 +1,8 @@
|
|||||||
"""Generic Plugwise Entity Class."""
|
"""Generic Plugwise Entity Class."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST
|
from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
@ -53,6 +55,11 @@ class PlugwiseEntity(CoordinatorEntity[PlugwiseData]):
|
|||||||
"""Return if entity is available."""
|
"""Return if entity is available."""
|
||||||
return super().available and self._dev_id in self.coordinator.data.devices
|
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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
self._handle_coordinator_update()
|
self._handle_coordinator_update()
|
||||||
|
@ -5,10 +5,10 @@ from typing import Any
|
|||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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 homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN, LOGGER
|
from .const import DOMAIN
|
||||||
from .coordinator import PlugwiseDataUpdateCoordinator
|
from .coordinator import PlugwiseDataUpdateCoordinator
|
||||||
from .entity import PlugwiseEntity
|
from .entity import PlugwiseEntity
|
||||||
from .util import plugwise_command
|
from .util import plugwise_command
|
||||||
@ -51,14 +51,19 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
|||||||
super().__init__(coordinator, device_id)
|
super().__init__(coordinator, device_id)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_unique_id = f"{device_id}-{description.key}"
|
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
|
@plugwise_command
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
await self.coordinator.api.set_switch_state(
|
await self.coordinator.api.set_switch_state(
|
||||||
self._dev_id,
|
self._dev_id,
|
||||||
self.coordinator.data.devices[self._dev_id].get("members"),
|
self.device.get("members"),
|
||||||
self.entity_description.key,
|
self.entity_description.key,
|
||||||
"on",
|
"on",
|
||||||
)
|
)
|
||||||
@ -68,18 +73,7 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
|||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
await self.coordinator.api.set_switch_state(
|
await self.coordinator.api.set_switch_state(
|
||||||
self._dev_id,
|
self._dev_id,
|
||||||
self.coordinator.data.devices[self._dev_id].get("members"),
|
self.device.get("members"),
|
||||||
self.entity_description.key,
|
self.entity_description.key,
|
||||||
"off",
|
"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()
|
|
||||||
|
@ -17,10 +17,10 @@ async def test_adam_climate_switch_entities(hass, mock_smile_adam):
|
|||||||
entry = await async_init_integration(hass, mock_smile_adam)
|
entry = await async_init_integration(hass, mock_smile_adam)
|
||||||
assert entry.state is ConfigEntryState.LOADED
|
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"
|
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"
|
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(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"turn_off",
|
"turn_off",
|
||||||
{"entity_id": "switch.cv_pomp"},
|
{"entity_id": "switch.cv_pomp_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ async def test_adam_climate_switch_negative_testing(hass, mock_smile_adam):
|
|||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"turn_on",
|
"turn_on",
|
||||||
{"entity_id": "switch.fibaro_hc2"},
|
{"entity_id": "switch.fibaro_hc2_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam):
|
|||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"turn_off",
|
"turn_off",
|
||||||
{"entity_id": "switch.cv_pomp"},
|
{"entity_id": "switch.cv_pomp_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam):
|
|||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"toggle",
|
"toggle",
|
||||||
{"entity_id": "switch.fibaro_hc2"},
|
{"entity_id": "switch.fibaro_hc2_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -89,7 +89,7 @@ async def test_adam_climate_switch_changes(hass, mock_smile_adam):
|
|||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"turn_on",
|
"turn_on",
|
||||||
{"entity_id": "switch.fibaro_hc2"},
|
{"entity_id": "switch.fibaro_hc2_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -104,10 +104,10 @@ async def test_stretch_switch_entities(hass, mock_stretch):
|
|||||||
entry = await async_init_integration(hass, mock_stretch)
|
entry = await async_init_integration(hass, mock_stretch)
|
||||||
assert entry.state is ConfigEntryState.LOADED
|
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"
|
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"
|
assert str(state.state) == "on"
|
||||||
|
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ async def test_stretch_switch_changes(hass, mock_stretch):
|
|||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"turn_off",
|
"turn_off",
|
||||||
{"entity_id": "switch.koelkast_92c4a"},
|
{"entity_id": "switch.koelkast_92c4a_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert mock_stretch.set_switch_state.call_count == 1
|
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(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"toggle",
|
"toggle",
|
||||||
{"entity_id": "switch.droger_52559"},
|
{"entity_id": "switch.droger_52559_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert mock_stretch.set_switch_state.call_count == 2
|
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(
|
await hass.services.async_call(
|
||||||
"switch",
|
"switch",
|
||||||
"turn_on",
|
"turn_on",
|
||||||
{"entity_id": "switch.droger_52559"},
|
{"entity_id": "switch.droger_52559_relay"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert mock_stretch.set_switch_state.call_count == 3
|
assert mock_stretch.set_switch_state.call_count == 3
|
||||||
|
Loading…
x
Reference in New Issue
Block a user