2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Homekit switches."""
|
2018-04-13 17:25:35 +00:00
|
|
|
import logging
|
|
|
|
|
2020-03-18 21:20:40 +00:00
|
|
|
from aiohomekit.model.characteristics import (
|
|
|
|
CharacteristicsTypes,
|
|
|
|
InUseValues,
|
|
|
|
IsConfiguredValues,
|
|
|
|
)
|
2019-08-14 16:14:15 +00:00
|
|
|
|
2018-04-13 17:25:35 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2020-01-29 21:59:45 +00:00
|
|
|
from homeassistant.core import callback
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2019-03-26 06:49:51 +00:00
|
|
|
from . import KNOWN_DEVICES, HomeKitEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-11-06 11:43:47 +00:00
|
|
|
OUTLET_IN_USE = "outlet_in_use"
|
|
|
|
|
2018-04-13 17:25:35 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class HomeKitSwitch(HomeKitEntity, SwitchDevice):
|
|
|
|
"""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
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
class HomeKitValve(HomeKitEntity, SwitchDevice):
|
|
|
|
"""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
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
|
ENTITY_TYPES = {
|
|
|
|
"switch": HomeKitSwitch,
|
|
|
|
"outlet": HomeKitSwitch,
|
|
|
|
"valve": HomeKitValve,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Homekit switches."""
|
|
|
|
hkid = config_entry.data["AccessoryPairingID"]
|
|
|
|
conn = hass.data[KNOWN_DEVICES][hkid]
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_service(aid, service):
|
|
|
|
entity_class = ENTITY_TYPES.get(service["stype"])
|
|
|
|
if not entity_class:
|
|
|
|
return False
|
|
|
|
info = {"aid": aid, "iid": service["iid"]}
|
|
|
|
async_add_entities([entity_class(conn, info)], True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.add_listener(async_add_service)
|