Skip to content

Commit

Permalink
Merge pull request #1 from jlusiardi/pypi
Browse files Browse the repository at this point in the history
Publish module on PyPi
  • Loading branch information
jlusiardi authored Sep 6, 2017
2 parents 33601d4 + 87b6a76 commit 99104de
Show file tree
Hide file tree
Showing 26 changed files with 1,950 additions and 244 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ ENV/
/.idea/workspace.xml
/.idea/inspectionProfiles/Project_Default.xml
/.idea/vcs.xml
/tmp
/tmp
/.idea/homekit_python.iml
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
# HomeKit client
# HomeKit Python

This code only works with HomeKit IP Accessories. no Bluetooth LE Accessories (yet)!
With this code it is possible to implement either a HomeKit Accessory or simulate a
HomeKit Controller.

**Limitations**

* This code only works with HomeKit IP Accessories. no Bluetooth LE Accessories (yet)!
* No reaction to events whatsoever.

The code presented in this repository was created based on release R1 from 2017-06-07.

# HomeKit Accessory
This package helps in creating a custom HomeKit Accessory.

The demonstration uses this JSON in `~/.homekit/demoserver.json`:
```json
{
"name": "DemoAccessory",
"host_ip": "$YOUR IP",
"host_port": 8080,
"accessory_pairing_id": "12:00:00:00:00:00",
"accessory_pin": "031-45-154",
"peers": {},
"unsuccessful_tries": 0
}
```

Now let's spawn a simple light bulb accessory as demonstration:

```python
#!/usr/bin/env python3

import os.path

from homekit import HomeKitServer
from homekit.model import Accessory, LightBulbService


if __name__ == '__main__':
try:
httpd = HomeKitServer(os.path.expanduser('~/.homekit/demoserver.json'))

accessory = Accessory('Licht')
lightService = LightBulbService()
accessory.services.append(lightService)
httpd.accessories.add_accessory(accessory)

httpd.publish_device()
print('published device and start serving')
httpd.serve_forever()
except KeyboardInterrupt:
print('unpublish device')
httpd.unpublish_device()
```

If everything went properly, you should be able to add this accessory to your home on your iOS device.

# HomeKit Controller

The following tools help to access HomeKit Accessories.

## discover.py

This tool will list all available HomeKit IP Accessories within the local network.
Expand All @@ -27,7 +83,7 @@ Status Flags (sf): 0
Category Identifier (ci): Other (Id: 1)
```

## identfy.py
## identify.py

This tool will use the Identify Routine of a HomeKit IP Accessory.

Expand Down Expand Up @@ -82,6 +138,8 @@ The option `-t` specifies if the type information should be read as well.

The option `-e` specifies if the event data should be read as well.

# HomeKit Accessory

# Tests

The code was tested with the following devices:
Expand Down
31 changes: 31 additions & 0 deletions demoserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3

import os.path

from homekit import HomeKitServer

from homekit.model import Accessory, LightBulbService


def light_switched(newval):
print('=======> light switched: {x}'.format(x=newval))


if __name__ == '__main__':
try:
httpd = HomeKitServer(os.path.expanduser('~/.homekit/demoserver.json'))

accessory = Accessory('Testlicht')
lightBulbService = LightBulbService()
lightBulbService.set_on_callback(light_switched)
accessory.services.append(lightBulbService)
httpd.accessories.add_accessory(accessory)

print(httpd.accessories.__str__())

httpd.publish_device()
print('published device and start serving')
httpd.serve_forever()
except KeyboardInterrupt:
print('unpublish device')
httpd.unpublish_device()
21 changes: 11 additions & 10 deletions homekit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import homekit.feature_flags
import homekit.categories
import homekit.characteristics
import homekit.services
import homekit.model.categories
import homekit.model.services
import homekit.statuscodes
import homekit.zeroconf
from homekit.tlv import TLV
from homekit.srp import SrpClient
from homekit.chacha20poly1305 import chacha20_aead_encrypt, chacha20_aead_decrypt
from homekit.protocol import perform_pair_setup, get_session_keys
from homekit.secure_http import SecureHttp
from homekit.server import HomeKitServer
from homekit.srp import SrpClient
from homekit.tlv import TLV
from homekit.tools import load_pairing, save_pairing
from homekit.protocol import perform_pair_setup, get_session_keys

# Init lookup objects
FeatureFlags = homekit.feature_flags.FeatureFlags
Categories = homekit.categories.Categories
StatusCodes = homekit.statuscodes.StatusCodes
CharacteristicsTypes = homekit.characteristics.CharacteristicsTypes
ServicesTypes = homekit.services.ServicesTypes
Categories = homekit.model.categories.Categories
HapStatusCodes = homekit.statuscodes.HapStatusCodes
HttpStatusCodes = homekit.statuscodes.HttpStatusCodes
CharacteristicsTypes = homekit.model.CharacteristicsTypes
ServicesTypes = homekit.model.services.ServicesTypes

discover_homekit_devices = homekit.zeroconf.discover_homekit_devices
find_device_ip_and_port = homekit.zeroconf.find_device_ip_and_port
141 changes: 0 additions & 141 deletions homekit/characteristics.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions identify.py → homekit/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import argparse
import http.client

from homekit import find_device_ip_and_port, StatusCodes
from homekit import find_device_ip_and_port, HapStatusCodes


def setup_args_parser():
Expand All @@ -26,7 +26,7 @@ def setup_args_parser():
if resp.code == 400:
data = json.loads(resp.read().decode())
code = data['status']
print('identify failed because: {reason} ({code}). Is it paired?'.format(reason=StatusCodes[code], code=code))
print('identify failed because: {reason} ({code}). Is it paired?'.format(reason=HapStatusCodes[code], code=code))
elif resp.code == 200:
print('identify succeeded.')
conn.close()
27 changes: 27 additions & 0 deletions homekit/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from homekit.model.characteristics import CharacteristicsTypes
from homekit.model.mixin import ToDictMixin, get_id
from homekit.model.services import AcessoryInformationService, LightBulbService, OutletService, FanService, \
ThermostatService
from homekit.model.categories import Categories


class Accessory(ToDictMixin):
def __init__(self, name):
self.aid = get_id()
self.services = [
AcessoryInformationService(name)
]

def get_name(self):
for service in self.services:
if isinstance(service, AcessoryInformationService):
return service.get_name()
return None


class Accessories(ToDictMixin):
def __init__(self):
self.accessories = []

def add_accessory(self, acessory: Accessory):
self.accessories.append(acessory)
File renamed without changes.
Loading

0 comments on commit 99104de

Please sign in to comment.