pyhap icon indicating copy to clipboard operation
pyhap copied to clipboard

Works in HomeKit app, but not via Automation

Open skela opened this issue 4 years ago • 0 comments

My custom devices work really well, and everything shows up in the HomeKit iOS app. The only thing I haven't been able to get working is Automations.

I've got one set up that should switch on a light when the main door of the house is opened. However, nothing happens. Clicking on the device in the HomeKit app works flawlessly though.

I've probably not set things up right or something with either the Bridge or the accessory class, but was just wondering if you had any hints or tips.

import asyncio
import logging
import signal
from pyhap.accessory_driver import AccessoryDriver

from pyhap.accessory import Accessory, Bridge
from pyhap.const import CATEGORY_LIGHTBULB
from xcomfort_manager import XComfortManager
from settings import Settings
from settings import XComfortDevice

class HomeKitXComfortLight(Accessory):

	category = CATEGORY_LIGHTBULB

	def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)

		settings = Settings()
		self.xcomfort = XComfortManager(settings.xcomfort)

		chars = [('On',self.set_on)]

		server = self.add_preload_service('Lightbulb', chars = [ name for (name,_) in chars ])

		for (name, setter) in chars:
			server.configure_char(name, setter_callback = setter)
		self.is_on = False

	def set_on(self, value):
		print(f"OMG value is {value}")
		if isinstance(value, str):
			if value == "1" or value.lower() == "on":
				self.is_on = True
			elif value == "0" or value.lower() == "off":
				self.is_on = False
			else:
				self.is_on = bool(value)
		else:
			self.is_on = bool(value)
		self.set_bulb()

	def set_bulb(self):
		if self.is_on:
			self.xcomfort.send_command(self.display_name,"on")
		else:
			self.xcomfort.send_command(self.display_name,"off")

	def stop(self):
		super().stop()

class HomeKitManager(object):

	def __init__(self, settings:Settings):
		self.settings = settings		
	
	def start(self):
		try:
			logging.basicConfig(level=logging.INFO)
			driver = AccessoryDriver(port=51827)
			bridge = Bridge(driver, 'Bridge')			
			for device in self.settings.xcomfort.devices:
				if not device.add_to_homekit:
					continue
				lamp = HomeKitXComfortLight(driver, device.name)
				bridge.add_accessory(lamp)
			driver.add_accessory(accessory=bridge)
			signal.signal(signal.SIGTERM, driver.signal_handler)
			driver.start()

		except KeyboardInterrupt:
			print('Stopping...')

Rest of the code can be found here if yer curious: https://github.com/skela/home

skela avatar Nov 23 '20 20:11 skela