2020-06-02 19:45:14 +00:00
|
|
|
"""Plugwise Switch component for HomeAssistant."""
|
2022-02-06 09:51:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2020-11-21 02:43:20 +00:00
|
|
|
from plugwise.exceptions import PlugwiseException
|
2020-06-02 19:45:14 +00:00
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2022-01-03 18:24:34 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-02 19:45:14 +00:00
|
|
|
|
2022-02-08 19:17:49 +00:00
|
|
|
from .const import DOMAIN, LOGGER, SWITCH_ICON
|
2022-02-06 17:03:50 +00:00
|
|
|
from .coordinator import PlugwiseDataUpdateCoordinator
|
2022-02-05 18:09:37 +00:00
|
|
|
from .entity import PlugwiseEntity
|
2020-06-02 19:45:14 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:24:34 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Set up the Smile switches from a config entry."""
|
2022-02-08 19:17:49 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
PlugwiseSwitchEntity(coordinator, device_id)
|
|
|
|
for device_id, device in coordinator.data.devices.items()
|
|
|
|
if "switches" in device and "relay" in device["switches"]
|
|
|
|
)
|
2020-06-02 19:45:14 +00:00
|
|
|
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Representation of a Plugwise plug."""
|
|
|
|
|
2022-02-06 09:51:50 +00:00
|
|
|
_attr_icon = SWITCH_ICON
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-08 10:13:05 +00:00
|
|
|
device_id: str,
|
2022-02-06 09:51:50 +00:00
|
|
|
) -> None:
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Set up the Plugwise API."""
|
2022-02-08 18:08:01 +00:00
|
|
|
super().__init__(coordinator, device_id)
|
2022-02-08 10:13:05 +00:00
|
|
|
self._attr_unique_id = f"{device_id}-plug"
|
|
|
|
self._members = coordinator.data.devices[device_id].get("members")
|
2022-02-06 09:51:50 +00:00
|
|
|
self._attr_is_on = False
|
2022-02-08 18:08:01 +00:00
|
|
|
self._attr_name = coordinator.data.devices[device_id].get("name")
|
2020-06-02 19:45:14 +00:00
|
|
|
|
2022-02-06 09:51:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Turn the device on."""
|
|
|
|
try:
|
2022-02-08 19:17:49 +00:00
|
|
|
state_on = await self.coordinator.api.set_switch_state(
|
2022-02-08 10:13:05 +00:00
|
|
|
self._dev_id, self._members, "relay", "on"
|
2020-11-08 17:09:43 +00:00
|
|
|
)
|
2020-11-21 02:43:20 +00:00
|
|
|
except PlugwiseException:
|
2022-02-05 23:43:05 +00:00
|
|
|
LOGGER.error("Error while communicating to device")
|
2022-02-06 09:51:50 +00:00
|
|
|
else:
|
|
|
|
if state_on:
|
|
|
|
self._attr_is_on = True
|
|
|
|
self.async_write_ha_state()
|
2020-06-02 19:45:14 +00:00
|
|
|
|
2022-02-06 09:51:50 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Turn the device off."""
|
|
|
|
try:
|
2022-02-08 19:17:49 +00:00
|
|
|
state_off = await self.coordinator.api.set_switch_state(
|
2022-02-08 10:13:05 +00:00
|
|
|
self._dev_id, self._members, "relay", "off"
|
2020-11-08 17:09:43 +00:00
|
|
|
)
|
2020-11-21 02:43:20 +00:00
|
|
|
except PlugwiseException:
|
2022-02-05 23:43:05 +00:00
|
|
|
LOGGER.error("Error while communicating to device")
|
2022-02-06 09:51:50 +00:00
|
|
|
else:
|
|
|
|
if state_off:
|
|
|
|
self._attr_is_on = False
|
|
|
|
self.async_write_ha_state()
|
2020-06-02 19:45:14 +00:00
|
|
|
|
|
|
|
@callback
|
2022-02-08 18:54:10 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
2022-02-08 10:13:05 +00:00
|
|
|
if not (data := self.coordinator.data.devices.get(self._dev_id)):
|
2022-02-08 18:08:01 +00:00
|
|
|
LOGGER.error("Received no data for device %s", self._dev_id)
|
2022-02-08 18:54:10 +00:00
|
|
|
super()._handle_coordinator_update()
|
2020-06-02 19:45:14 +00:00
|
|
|
return
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
self._attr_is_on = data["switches"].get("relay")
|
2022-02-08 18:54:10 +00:00
|
|
|
super()._handle_coordinator_update()
|