- Notifications
You must be signed in to change notification settings - Fork121
A python implementation of the HomeKit Accessory Protocol (HAP)
License
ikalchev/HAP-python
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
HomeKit Accessory Protocol implementation in python 3.With this project, you can integrate your own smart devices and add them to youriOS Home app. Since Siri is integrated with the Home app, you can start voice-control youraccessories right away.
Main features:
- Camera - HAP-python supports the camera accessory from version 2.3.0!
- asyncio support - You can run various tasks or accessories in the event loop.
- Out of the box support for Apple-defined services - see them inthe resources folder.
- Secure pairing by just scanning the QR code.
- Integrated with the home automation frameworkHome Assistant.
The project was developed for a Raspberry Pi, but it should work on other platforms. To kick-start things,you can openmain.py orbusy_home.py, where you will find some fake accessories.Just run one of them, for examplepython3 busy_home.py, and you can add it inthe Home app (be sure to be in the same network).Stop it by hittingCtrl+C.
There are example accessories as well as integrations with real productsinthe accessories folder. See how to configure your camera incamera_main.py.
As of version 3.5.1, HAP-python no longer supports python older than 3.6, because weare moving to asyncio. If your platform does not have a compatible python out of thebox, you can install it manually or just use an older version of HAP-python.
As a prerequisite, you will need Avahi/Bonjour installed (due to zeroconf package).On a Raspberry Pi, you can get it with:
$ sudo apt-get install libavahi-compat-libdnssd-devavahi-utils may also fit the bill. Then, you can install withpip3 (you will needsudo or--user for the install):
$ pip3 install HAP-python[QRCode]
This will install HAP-python in your python packages, so that you can import it aspyhap. To uninstall, just do:
$ pip3 uninstall HAP-pythonA typical flow for using HAP-python starts with implementing an Accessory. This is done bysubclassingAccessory and putting in place a few details(see below). After that, you give your accessory to an AccessoryDriver to manage. Thiswill take care of advertising it on the local network, setting a HAP server andrunning the Accessory. Take a look atmain.py for a quick start on that.
frompyhap.accessoryimportAccessory,Categoryimportpyhap.loaderasloaderclassTemperatureSensor(Accessory):"""Implementation of a mock temperature sensor accessory."""category=Category.SENSOR# This is for the icon in the iOS Home app.def__init__(self,*args,**kwargs):"""Here, we just store a reference to the current temperature characteristic and add a method that will be executed every time its value changes. """# If overriding this method, be sure to call the super's implementation first.super().__init__(*args,**kwargs)# Add the services that this Accessory will support with add_preload_service heretemp_service=self.add_preload_service('TemperatureSensor')self.temp_char=temp_service.get_characteristic('CurrentTemperature')# Having a callback is optional, but you can use it to add functionality.self.temp_char.setter_callback=self.temperature_changeddeftemperature_changed(self,value):"""This will be called every time the value of the CurrentTemperature is changed. Use setter_callbacks to react to user actions, e.g. setting the lights On could fire some GPIO code to turn on a LED (see pyhap/accessories/LightBulb.py). """print('Temperature changed to: ',value)@Accessory.run_at_interval(3)# Run this method every 3 seconds# The `run` method can be `async` as welldefrun(self):"""We override this method to implement what the accessory will do when it is started. We set the current temperature to a random number. The decorator runs this method every 3 seconds. """self.temp_char.set_value(random.randint(18,26))# The `stop` method can be `async` as welldefstop(self):"""We override this method to clean up any resources or perform final actions, as this is called by the AccessoryDriver when the Accessory is being stopped. """print('Stopping accessory.')
When you are working with tightly coupled characteristics such as "On" and "Brightness,"you may need to use a service callback to receive all changes in a single request.
With characteristic callbacks, you do now know that a "Brightness" characteristic isabout to be processed right after an "On" and may end up setting a LightBulb to 100%and then dim it back down to the expected level.
frompyhap.accessoryimportAccessoryfrompyhap.constimportCategoryimportpyhap.loaderasloaderclassLight(Accessory):"""Implementation of a mock light accessory."""category=Category.CATEGORY_LIGHTBULB# This is for the icon in the iOS Home app.def__init__(self,*args,**kwargs):"""Here, we just store a reference to the on and brightness characteristics and add a method that will be executed every time their value changes. """# If overriding this method, be sure to call the super's implementation first.super().__init__(*args,**kwargs)# Add the services that this Accessory will support with add_preload_service hereserv_light=self.add_preload_service('Lightbulb')self.char_on=serv_light.configure_char('On',value=self._state)self.char_brightness=serv_light.configure_char('Brightness',value=100)serv_light.setter_callback=self._set_charsdef_set_chars(self,char_values):"""This will be called every time the value of the on of the characteristics on the service changes. """if"On"inchar_values:print('On changed to: ',char_values["On"])if"Brightness"inchar_values:print('Brightness changed to: ',char_values["Brightness"])@Accessory.run_at_interval(3)# Run this method every 3 seconds# The `run` method can be `async` as welldefrun(self):"""We override this method to implement what the accessory will do when it is started. We set the current temperature to a random number. The decorator runs this method every 3 seconds. """self.char_on.set_value(random.randint(0,1))self.char_brightness.set_value(random.randint(1,100))# The `stop` method can be `async` as welldefstop(self):"""We override this method to clean up any resources or perform final actions, as this is called by the AccessoryDriver when the Accessory is being stopped. """print('Stopping accessory.')
TheCamera accessory implements the HomeKit Protocol for negotiating stream settings,such as the picture width and height, number of audio channels and others.Starting a video and/or audio stream is very platform specific. Because of this,you need to figure out what video and audio settings your camera supports and set themin theoptions parameter that is passed to theCamera Accessory. Refer to thedocumentation for theCamera contructor for the settings you need to specify.
By default, HAP-python will execute theffmpeg command with the negotiated parameterswhen the stream should be started and willterminate the started process when thestream should be stopped (see the default:Camera.FFMPEG_CMD).If the default command is not supported or correctly formatted for your platform,the streaming can fail.
For these cases, HAP-python has hooks so that you can insert your own command or implementthe logic for starting or stopping the stream. There are two options:
Pass your own command that will be executed when the stream should be started.
You pass the command as a value to the key
start_stream_cmdin theoptionsparameter tothe constuctor of theCameraAccessory. The command is formatted using thenegotiated stream configuration parameters. For example, if the negotiated widthis 640 and you passfoo start -width {width}, the command will be formatted asfoo start -width 640.The full list of negotiated stream configuration parameters can be found in thedocumentation for the
Camera.startmethod.Implement your own logic to start, stop and reconfigure the stream.
If you need more flexibility in managing streams, you can directly implement the
Cameramethodsstart,stopandreconfigure. Each will be called when thestream should be respectively started, stopped or reconfigured. The start andreconfigure methods are given the negotiated stream configuration parameters.Have a look at the documentation of these methods for more information.
Finally, if you can take snapshots from the camera, you may want to implement theCamera.snapshot method. By default, this serves a stock photo.
This is a quick way to getHAP-python to run at boot on a Raspberry Pi. It is recommendedto turn on "Wait for network" inraspi-config. If this turns to be unreliable, seethis.
Copy the below in/etc/systemd/system/HAP-python.service (needs sudo).
[Unit]Description = HAP-python daemonWants = pigpiod.service # Remove this if you don't depend on pigpiodAfter = local-fs.target network-online.target pigpiod.service[Service]User = lesserdaemon # It's a good idea to use some unprivileged system user# Script starting HAP-python, e.g. main.py# Be careful to set any paths you use, e.g. for persisting the state.ExecStart = /usr/bin/python3 /home/lesserdaemon/.hap-python/hap-python.py[Install]WantedBy = multi-user.targetTest that everything is fine by doing:
> sudo systemctl start HAP-python> systemctl status HAP-python> sudo journalctl -u HAP-python# to see the output of the start up script.> sudo systemctl stop HAP-python
To enable or disable at boot, do:
> sudo systemctlenable HAP-python> sudo systemctl disable HAP-python
If you are runningHAP-python on a Raspberry Pi, you may want to add aShutdown Switch to your Home. This is aSwitch Accessory, which, when triggered, executessudo shutdown -h now, i.e.it shutdowns and halts the Pi. This allows you to safely unplug it.
For the above to work, you need to enable passwordless/sbin/shutdown to whicheveruser is runningHAP-python. For example, do:
$ sudo visudo# and add the line: "<hap-user> ALL=NOPASSWD: /sbin/shutdown".Some HAP know-how was taken fromHAP-NodeJS by KhaosT.
I am not aware of any bugs, but I am more than confident that such exist. If you find any,please report and I will try to fix them.
Suggestions are always welcome.
Have fun!
About
A python implementation of the HomeKit Accessory Protocol (HAP)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.