Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Works in HomeKit app, but not via Automation #3

Open
skela opened this issue Nov 23, 2020 · 0 comments
Open

Works in HomeKit app, but not via Automation #3

skela opened this issue Nov 23, 2020 · 0 comments

Comments

@skela
Copy link

skela commented Nov 23, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant