diff --git a/pytradfri/__main__.py b/pytradfri/__main__.py index eb942e28..e537ae9f 100644 --- a/pytradfri/__main__.py +++ b/pytradfri/__main__.py @@ -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 @@ -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) @@ -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()) @@ -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))") diff --git a/pytradfri/const.py b/pytradfri/const.py index 97a9bc02..b8821fa0 100644 --- a/pytradfri/const.py +++ b/pytradfri/const.py @@ -47,3 +47,5 @@ ATTR_TRANSITION_TIME = "5712" ATTR_REPEAT_DAYS = "9041" + +ATTR_HOMEKIT_ID = "9083" diff --git a/pytradfri/gateway.py b/pytradfri/gateway.py index 6f177311..4d4875aa 100644 --- a/pytradfri/gateway.py +++ b/pytradfri/gateway.py @@ -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 @@ -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] diff --git a/tests/test_gateway.py b/tests/test_gateway.py index 5135467f..e490910a 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -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(): @@ -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']