2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Homekit switches."""
|
2021-12-22 18:49:58 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2020-03-18 21:20:40 +00:00
|
|
|
from aiohomekit.model.characteristics import (
|
2021-12-22 18:49:58 +00:00
|
|
|
Characteristic,
|
2020-03-18 21:20:40 +00:00
|
|
|
CharacteristicsTypes,
|
|
|
|
InUseValues,
|
|
|
|
IsConfiguredValues,
|
|
|
|
)
|
2020-11-16 23:11:39 +00:00
|
|
|
from aiohomekit.model.services import ServicesTypes
|
2019-08-14 16:14:15 +00:00
|
|
|
|
2021-12-22 18:49:58 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
2021-12-28 18:24:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-12-22 18:49:58 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-12-28 18:24:40 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2021-12-22 18:49:58 +00:00
|
|
|
from . import KNOWN_DEVICES, CharacteristicEntity, HomeKitEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-11-06 11:43:47 +00:00
|
|
|
OUTLET_IN_USE = "outlet_in_use"
|
|
|
|
|
2020-03-18 21:20:40 +00:00
|
|
|
ATTR_IN_USE = "in_use"
|
|
|
|
ATTR_IS_CONFIGURED = "is_configured"
|
|
|
|
ATTR_REMAINING_DURATION = "remaining_duration"
|
2018-04-13 17:25:35 +00:00
|
|
|
|
|
|
|
|
2021-12-22 18:49:58 +00:00
|
|
|
@dataclass
|
|
|
|
class DeclarativeSwitchEntityDescription(SwitchEntityDescription):
|
|
|
|
"""Describes Homekit button."""
|
|
|
|
|
|
|
|
true_value: bool = True
|
|
|
|
false_value: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_ENTITIES: dict[str, DeclarativeSwitchEntityDescription] = {
|
2022-01-31 22:48:16 +00:00
|
|
|
CharacteristicsTypes.VENDOR_AQARA_PAIRING_MODE: DeclarativeSwitchEntityDescription(
|
|
|
|
key=CharacteristicsTypes.VENDOR_AQARA_PAIRING_MODE,
|
2021-12-22 18:49:58 +00:00
|
|
|
name="Pairing Mode",
|
|
|
|
icon="mdi:lock-open",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
),
|
2022-01-31 22:48:16 +00:00
|
|
|
CharacteristicsTypes.VENDOR_AQARA_E1_PAIRING_MODE: DeclarativeSwitchEntityDescription(
|
|
|
|
key=CharacteristicsTypes.VENDOR_AQARA_E1_PAIRING_MODE,
|
2021-12-22 18:49:58 +00:00
|
|
|
name="Pairing Mode",
|
|
|
|
icon="mdi:lock-open",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class HomeKitSwitch(HomeKitEntity, SwitchEntity):
|
2018-04-13 17:25:35 +00:00
|
|
|
"""Representation of a Homekit switch."""
|
|
|
|
|
2019-01-28 16:21:20 +00:00
|
|
|
def get_characteristic_types(self):
|
|
|
|
"""Define the homekit characteristics the entity cares about."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [CharacteristicsTypes.ON, CharacteristicsTypes.OUTLET_IN_USE]
|
2019-01-28 16:21:20 +00:00
|
|
|
|
2018-04-13 17:25:35 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
2020-03-11 11:40:47 +00:00
|
|
|
return self.service.value(CharacteristicsTypes.ON)
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-04-13 17:25:35 +00:00
|
|
|
"""Turn the specified switch on."""
|
2020-03-11 16:27:20 +00:00
|
|
|
await self.async_put_characteristics({CharacteristicsTypes.ON: True})
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-04-13 17:25:35 +00:00
|
|
|
"""Turn the specified switch off."""
|
2020-03-11 16:27:20 +00:00
|
|
|
await self.async_put_characteristics({CharacteristicsTypes.ON: False})
|
2018-11-06 11:43:47 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-11-06 11:43:47 +00:00
|
|
|
"""Return the optional state attributes."""
|
2020-03-11 11:40:47 +00:00
|
|
|
outlet_in_use = self.service.value(CharacteristicsTypes.OUTLET_IN_USE)
|
|
|
|
if outlet_in_use is not None:
|
|
|
|
return {OUTLET_IN_USE: outlet_in_use}
|
2020-03-18 21:20:40 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class HomeKitValve(HomeKitEntity, SwitchEntity):
|
2020-03-18 21:20:40 +00:00
|
|
|
"""Represents a valve in an irrigation system."""
|
|
|
|
|
|
|
|
def get_characteristic_types(self):
|
|
|
|
"""Define the homekit characteristics the entity cares about."""
|
|
|
|
return [
|
|
|
|
CharacteristicsTypes.ACTIVE,
|
|
|
|
CharacteristicsTypes.IN_USE,
|
|
|
|
CharacteristicsTypes.IS_CONFIGURED,
|
|
|
|
CharacteristicsTypes.REMAINING_DURATION,
|
|
|
|
]
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the specified valve on."""
|
|
|
|
await self.async_put_characteristics({CharacteristicsTypes.ACTIVE: True})
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn the specified valve off."""
|
|
|
|
await self.async_put_characteristics({CharacteristicsTypes.ACTIVE: False})
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon."""
|
|
|
|
return "mdi:water"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self.service.value(CharacteristicsTypes.ACTIVE)
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-03-18 21:20:40 +00:00
|
|
|
"""Return the optional state attributes."""
|
|
|
|
attrs = {}
|
|
|
|
|
|
|
|
in_use = self.service.value(CharacteristicsTypes.IN_USE)
|
|
|
|
if in_use is not None:
|
|
|
|
attrs[ATTR_IN_USE] = in_use == InUseValues.IN_USE
|
|
|
|
|
|
|
|
is_configured = self.service.value(CharacteristicsTypes.IS_CONFIGURED)
|
|
|
|
if is_configured is not None:
|
|
|
|
attrs[ATTR_IS_CONFIGURED] = is_configured == IsConfiguredValues.CONFIGURED
|
|
|
|
|
|
|
|
remaining = self.service.value(CharacteristicsTypes.REMAINING_DURATION)
|
|
|
|
if remaining is not None:
|
|
|
|
attrs[ATTR_REMAINING_DURATION] = remaining
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
2021-12-22 18:49:58 +00:00
|
|
|
class DeclarativeCharacteristicSwitch(CharacteristicEntity, SwitchEntity):
|
|
|
|
"""Representation of a Homekit switch backed by a single characteristic."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
conn,
|
|
|
|
info,
|
|
|
|
char,
|
|
|
|
description: DeclarativeSwitchEntityDescription,
|
|
|
|
):
|
|
|
|
"""Initialise a HomeKit switch."""
|
|
|
|
self.entity_description = description
|
|
|
|
super().__init__(conn, info, char)
|
|
|
|
|
|
|
|
@property
|
2022-02-01 11:27:35 +00:00
|
|
|
def name(self) -> str | None:
|
2021-12-22 18:49:58 +00:00
|
|
|
"""Return the name of the device if any."""
|
|
|
|
if prefix := super().name:
|
|
|
|
return f"{prefix} {self.entity_description.name}"
|
|
|
|
return self.entity_description.name
|
|
|
|
|
|
|
|
def get_characteristic_types(self):
|
|
|
|
"""Define the homekit characteristics the entity cares about."""
|
|
|
|
return [self._char.type]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._char.value == self.entity_description.true_value
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the specified switch on."""
|
|
|
|
await self.async_put_characteristics(
|
|
|
|
{self._char.type: self.entity_description.true_value}
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn the specified switch off."""
|
|
|
|
await self.async_put_characteristics(
|
|
|
|
{self._char.type: self.entity_description.false_value}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-18 21:20:40 +00:00
|
|
|
ENTITY_TYPES = {
|
2020-11-16 23:11:39 +00:00
|
|
|
ServicesTypes.SWITCH: HomeKitSwitch,
|
|
|
|
ServicesTypes.OUTLET: HomeKitSwitch,
|
|
|
|
ServicesTypes.VALVE: HomeKitValve,
|
2020-03-18 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 18:24:40 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-03-18 21:20:40 +00:00
|
|
|
"""Set up Homekit switches."""
|
|
|
|
hkid = config_entry.data["AccessoryPairingID"]
|
|
|
|
conn = hass.data[KNOWN_DEVICES][hkid]
|
|
|
|
|
|
|
|
@callback
|
2020-11-16 23:11:39 +00:00
|
|
|
def async_add_service(service):
|
2022-01-31 22:48:16 +00:00
|
|
|
if not (entity_class := ENTITY_TYPES.get(service.type)):
|
2020-03-18 21:20:40 +00:00
|
|
|
return False
|
2020-11-16 23:11:39 +00:00
|
|
|
info = {"aid": service.accessory.aid, "iid": service.iid}
|
2020-03-18 21:20:40 +00:00
|
|
|
async_add_entities([entity_class(conn, info)], True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.add_listener(async_add_service)
|
2021-12-22 18:49:58 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_characteristic(char: Characteristic):
|
|
|
|
if not (description := SWITCH_ENTITIES.get(char.type)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
|
|
|
|
async_add_entities(
|
|
|
|
[DeclarativeCharacteristicSwitch(conn, info, char, description)], True
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.add_char_factory(async_add_characteristic)
|