Skip to content

Commit

Permalink
Update CLI to support 4.0 and expose Homekit key (#100)
Browse files Browse the repository at this point in the history
* Update CLI to support 4.0
* Added Homekit ID
* Adding more tests for Gateway
  • Loading branch information
Patrik authored Nov 4, 2017
1 parent 7f6f981 commit c6f08bf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
18 changes: 16 additions & 2 deletions pytradfri/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

from .const import * # noqa
from .api.libcoap_api import api_factory
from pytradfri.api.libcoap_api import APIFactory
from .gateway import Gateway
from .command import Command

Expand All @@ -16,7 +16,19 @@

logging.basicConfig(level=logging.DEBUG)

api = api_factory(sys.argv[1], sys.argv[2])
api_factory = APIFactory(sys.argv[1])
with open('gateway_psk.txt', 'a+') as file:
file.seek(0)
psk = file.read()
if psk:
api_factory.psk = psk.strip()
else:
psk = api_factory.generate_psk(sys.argv[2])
print('Generated PSK: ', psk)
file.write(psk)

api = api_factory.request

gateway = Gateway()
devices_commands = api(gateway.get_devices())
devices = api(devices_commands)
Expand All @@ -27,6 +39,7 @@
moods = api(gateway.get_moods())
mood = moods[0]
tasks = api(gateway.get_smart_tasks())
homekit_id = api(gateway.get_gateway_info()).homekit_id

def dump_all():
endpoints = api(gateway.get_endpoints())
Expand All @@ -47,6 +60,7 @@ def dump_devices():
print()
print("Example commands:")
print("> devices")
print("> homekit_id")
print("> light.light_control.lights")
print("> api(light.light_control.set_dimmer(10))")
print("> api(light.light_control.set_dimmer(254))")
Expand Down
2 changes: 2 additions & 0 deletions pytradfri/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@

ATTR_TRANSITION_TIME = "5712"
ATTR_REPEAT_DAYS = "9041"

ATTR_HOMEKIT_ID = "9083"
6 changes: 5 additions & 1 deletion pytradfri/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ATTR_CURRENT_TIME_UNIX, ATTR_CURRENT_TIME_ISO8601,
ATTR_FIRST_SETUP, ATTR_GATEWAY_INFO, ATTR_GATEWAY_ID,
ATTR_GATEWAY_REBOOT, ATTR_GATEWAY_FACTORY_DEFAULTS,
ATTR_AUTH, ATTR_IDENTITY, ATTR_PSK)
ATTR_AUTH, ATTR_IDENTITY, ATTR_PSK, ATTR_HOMEKIT_ID)
from .device import Device
from .group import Group
from .mood import Mood
Expand Down Expand Up @@ -227,6 +227,10 @@ def first_setup(self):
return None
return datetime.utcfromtimestamp(self.raw[ATTR_FIRST_SETUP])

@property
def homekit_id(self):
return self.raw.get(ATTR_HOMEKIT_ID)

@property
def path(self):
return [ROOT_GATEWAY, ATTR_GATEWAY_INFO]
Expand Down
45 changes: 44 additions & 1 deletion tests/test_gateway.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
from datetime import datetime

from pytradfri.const import ROOT_DEVICES
from pytradfri.gateway import Gateway
from pytradfri.gateway import Gateway, GatewayInfo


GATEWAY = {
"9023": "xyz.pool.ntp.pool",
"9029": "1.2.42",
"9054": 0,
"9055": 0,
"9059": 1509788799,
"9060": "2017-11-04T09:46:39.046784Z",
"9061": 0,
"9062": 0,
"9066": 5,
"9069": 1509474847,
"9071": 1,
"9072": 0,
"9073": 0,
"9074": 0,
"9075": 0,
"9076": 0,
"9077": 0,
"9078": 0,
"9079": 0,
"9080": 0,
"9081": "7e0000000000000a",
"9083": "123-45-67",
"9092": 0,
"9093": 0,
"9106": 0
}


def test_get_device():
Expand All @@ -8,3 +39,15 @@ def test_get_device():

assert command.method == 'get'
assert command.path == [ROOT_DEVICES, 123]


def test_gateway_info():
gateway_info = GatewayInfo(GATEWAY)

assert gateway_info.id == '7e0000000000000a'
assert gateway_info.ntp_server == 'xyz.pool.ntp.pool'
assert gateway_info.firmware_version == '1.2.42'
assert gateway_info.current_time_iso8601 == '2017-11-04T09:46:39.046784Z'
assert gateway_info.first_setup == datetime.utcfromtimestamp(1509474847)
assert gateway_info.homekit_id == '123-45-67'
assert gateway_info.path == ['15011', '15012']

0 comments on commit c6f08bf

Please sign in to comment.