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
|
|
|
|
|
|
|
|
from plugwise import Smile
|
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
|
2022-02-06 09:51:50 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2020-06-02 19:45:14 +00:00
|
|
|
|
2022-02-05 23:43:05 +00:00
|
|
|
from .const import COORDINATOR, DOMAIN, LOGGER, SWITCH_ICON
|
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."""
|
|
|
|
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
2020-09-06 20:26:14 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
|
2020-06-02 19:45:14 +00:00
|
|
|
|
|
|
|
entities = []
|
2020-11-08 17:09:43 +00:00
|
|
|
switch_classes = ["plug", "switch_group"]
|
|
|
|
|
2020-06-02 19:45:14 +00:00
|
|
|
all_devices = api.get_all_devices()
|
|
|
|
for dev_id, device_properties in all_devices.items():
|
2020-11-08 17:09:43 +00:00
|
|
|
members = None
|
|
|
|
model = None
|
|
|
|
|
|
|
|
if any(
|
|
|
|
switch_class in device_properties["types"]
|
|
|
|
for switch_class in switch_classes
|
|
|
|
):
|
|
|
|
if "plug" in device_properties["types"]:
|
|
|
|
model = "Metered Switch"
|
|
|
|
if "switch_group" in device_properties["types"]:
|
|
|
|
members = device_properties["members"]
|
|
|
|
model = "Switch Group"
|
|
|
|
|
2020-06-02 19:45:14 +00:00
|
|
|
entities.append(
|
2020-11-21 02:43:20 +00:00
|
|
|
GwSwitch(
|
2020-11-08 17:09:43 +00:00
|
|
|
api, coordinator, device_properties["name"], dev_id, members, model
|
|
|
|
)
|
2020-06-02 19:45:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2022-02-05 18:09:37 +00:00
|
|
|
class GwSwitch(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,
|
|
|
|
api: Smile,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
name: str,
|
|
|
|
dev_id: str,
|
|
|
|
members: list[str] | None,
|
|
|
|
model: str | None,
|
|
|
|
) -> None:
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Set up the Plugwise API."""
|
|
|
|
super().__init__(api, coordinator, name, dev_id)
|
2022-02-05 22:59:37 +00:00
|
|
|
self._attr_unique_id = f"{dev_id}-plug"
|
2020-06-02 19:45:14 +00:00
|
|
|
|
2020-11-08 17:09:43 +00:00
|
|
|
self._members = members
|
2020-06-02 19:45:14 +00:00
|
|
|
self._model = model
|
|
|
|
|
2022-02-06 09:51:50 +00:00
|
|
|
self._attr_is_on = False
|
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:
|
2020-11-08 17:09:43 +00:00
|
|
|
state_on = await self._api.set_relay_state(
|
|
|
|
self._dev_id, self._members, "on"
|
|
|
|
)
|
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:
|
2020-11-08 17:09:43 +00:00
|
|
|
state_off = await self._api.set_relay_state(
|
|
|
|
self._dev_id, self._members, "off"
|
|
|
|
)
|
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-06 09:51:50 +00:00
|
|
|
def _async_process_data(self) -> None:
|
2020-06-02 19:45:14 +00:00
|
|
|
"""Update the data from the Plugs."""
|
2021-10-31 17:32:17 +00:00
|
|
|
if not (data := self._api.get_device_data(self._dev_id)):
|
2022-02-05 23:43:05 +00:00
|
|
|
LOGGER.error("Received no data for device %s", self._name)
|
2020-06-02 19:45:14 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
|
|
|
if "relay" in data:
|
2022-02-06 09:51:50 +00:00
|
|
|
self._attr_is_on = data["relay"]
|
2020-06-02 19:45:14 +00:00
|
|
|
|
|
|
|
self.async_write_ha_state()
|