# pylint: disable=no-value-for-parameter
import logging
from aiohap import Accessory, Category, services
from .base import Link
logger = logging.getLogger(__name__)
[docs]class SwitchAccessory(Accessory):
category = Category.SWITCH
info = services.AccessoryInformation
switch = services.Switch
[docs]class SwitchLink(Link):
meadow_service_type = 'switch'
def _create_link(self):
super()._create_link()
self._md_switch = self._md_service
self._hk_switch = SwitchAccessory(self._md_switch.name)
self._fill_accessory_info(self._hk_switch)
self._hk_switch.switch.set(on=False)
self.bridge.accessory_server.add_accessory(self._hk_switch)
async def _setup(self):
self._hk_switch.switch.on.handle_write(self._handle_accessory_write)
self._md_switch.is_on.subscribe(self._handle_meadow_notification)
async def _reload_state(self):
await self._md_switch.is_on.enable_notifications()
self._hk_switch.switch.on.value = await self._md_switch.is_on.read(timeout=5.0)
async def _handle_meadow_notification(self, value, **kwargs):
self._hk_switch.switch.on.value = value
async def _handle_accessory_write(self, value, **kwargs):
await self._md_switch.is_on.write(bool(value), timeout=5.0)
self._hk_switch.switch.on.value = value