2020-06-02 21:45:14 +02:00
|
|
|
"""Plugwise Switch component for HomeAssistant."""
|
2022-02-06 10:51:50 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2022-02-10 15:49:19 +01:00
|
|
|
from homeassistant.components.switch import (
|
|
|
|
SwitchDeviceClass,
|
|
|
|
SwitchEntity,
|
|
|
|
SwitchEntityDescription,
|
|
|
|
)
|
2022-01-03 19:24:34 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-02-10 10:17:37 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-02-10 15:49:19 +01:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2022-01-03 19:24:34 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-02 21:45:14 +02:00
|
|
|
|
2022-02-10 10:17:37 +01:00
|
|
|
from .const import DOMAIN
|
2022-02-06 18:03:50 +01:00
|
|
|
from .coordinator import PlugwiseDataUpdateCoordinator
|
2022-02-05 19:09:37 +01:00
|
|
|
from .entity import PlugwiseEntity
|
2022-02-10 09:53:26 +01:00
|
|
|
from .util import plugwise_command
|
2020-06-02 21:45:14 +02:00
|
|
|
|
2022-02-09 17:56:07 +01:00
|
|
|
SWITCHES: tuple[SwitchEntityDescription, ...] = (
|
2022-02-10 15:49:19 +01:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key="dhw_cm_switch",
|
2022-07-20 11:38:00 +02:00
|
|
|
name="DHW comfort mode",
|
2022-02-10 15:49:19 +01:00
|
|
|
icon="mdi:water-plus",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
),
|
|
|
|
SwitchEntityDescription(
|
|
|
|
key="lock",
|
|
|
|
name="Lock",
|
|
|
|
icon="mdi:lock",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
),
|
2022-02-09 17:56:07 +01:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key="relay",
|
|
|
|
name="Relay",
|
2022-02-10 15:49:19 +01:00
|
|
|
device_class=SwitchDeviceClass.SWITCH,
|
2022-02-09 17:56:07 +01:00
|
|
|
),
|
2022-10-19 11:26:27 +02:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key="cooling_ena_switch",
|
|
|
|
name="Cooling",
|
|
|
|
icon="mdi:snowflake-thermometer",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
),
|
2022-02-09 17:56:07 +01:00
|
|
|
)
|
|
|
|
|
2020-06-02 21:45:14 +02:00
|
|
|
|
2022-01-03 19:24:34 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-02 21:45:14 +02:00
|
|
|
"""Set up the Smile switches from a config entry."""
|
2022-02-08 20:17:49 +01:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2022-02-09 17:56:07 +01:00
|
|
|
entities: list[PlugwiseSwitchEntity] = []
|
|
|
|
for device_id, device in coordinator.data.devices.items():
|
|
|
|
for description in SWITCHES:
|
|
|
|
if "switches" not in device or description.key not in device["switches"]:
|
|
|
|
continue
|
|
|
|
entities.append(PlugwiseSwitchEntity(coordinator, device_id, description))
|
|
|
|
async_add_entities(entities)
|
2020-06-02 21:45:14 +02:00
|
|
|
|
|
|
|
|
2022-02-08 11:13:05 +01:00
|
|
|
class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
2020-06-02 21:45:14 +02:00
|
|
|
"""Representation of a Plugwise plug."""
|
|
|
|
|
2022-02-06 10:51:50 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-02-06 18:03:50 +01:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-08 11:13:05 +01:00
|
|
|
device_id: str,
|
2022-02-09 17:56:07 +01:00
|
|
|
description: SwitchEntityDescription,
|
2022-02-06 10:51:50 +01:00
|
|
|
) -> None:
|
2020-06-02 21:45:14 +02:00
|
|
|
"""Set up the Plugwise API."""
|
2022-02-08 19:08:01 +01:00
|
|
|
super().__init__(coordinator, device_id)
|
2022-02-09 17:56:07 +01:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"{device_id}-{description.key}"
|
2022-02-10 10:17:37 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self.device["switches"].get(self.entity_description.key)
|
2020-06-02 21:45:14 +02:00
|
|
|
|
2022-02-10 09:53:26 +01:00
|
|
|
@plugwise_command
|
2022-02-06 10:51:50 +01:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-06-02 21:45:14 +02:00
|
|
|
"""Turn the device on."""
|
2022-02-10 09:53:26 +01:00
|
|
|
await self.coordinator.api.set_switch_state(
|
|
|
|
self._dev_id,
|
2022-02-10 10:17:37 +01:00
|
|
|
self.device.get("members"),
|
2022-02-10 09:53:26 +01:00
|
|
|
self.entity_description.key,
|
|
|
|
"on",
|
|
|
|
)
|
2020-06-02 21:45:14 +02:00
|
|
|
|
2022-02-10 09:53:26 +01:00
|
|
|
@plugwise_command
|
2022-02-06 10:51:50 +01:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-06-02 21:45:14 +02:00
|
|
|
"""Turn the device off."""
|
2022-02-10 09:53:26 +01:00
|
|
|
await self.coordinator.api.set_switch_state(
|
|
|
|
self._dev_id,
|
2022-02-10 10:17:37 +01:00
|
|
|
self.device.get("members"),
|
2022-02-10 09:53:26 +01:00
|
|
|
self.entity_description.key,
|
|
|
|
"off",
|
|
|
|
)
|