2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Homekit switches."""
|
2018-04-13 17:25:35 +00:00
|
|
|
import logging
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
from aiohomekit.model.characteristics import CharacteristicsTypes
|
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__)
|
|
|
|
|
|
|
|
|
2019-05-13 06:56:05 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Homekit lock."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hkid = config_entry.data["AccessoryPairingID"]
|
2019-05-13 06:56:05 +00:00
|
|
|
conn = hass.data[KNOWN_DEVICES][hkid]
|
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
@callback
|
2019-05-13 06:56:05 +00:00
|
|
|
def async_add_service(aid, service):
|
2019-07-31 19:25:30 +00:00
|
|
|
if service["stype"] not in ("switch", "outlet"):
|
2019-05-13 06:56:05 +00:00
|
|
|
return False
|
2019-07-31 19:25:30 +00:00
|
|
|
info = {"aid": aid, "iid": service["iid"]}
|
2019-05-13 06:56:05 +00:00
|
|
|
async_add_entities([HomeKitSwitch(conn, info)], True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.add_listener(async_add_service)
|
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}
|