From 672d0a222fb030534fbb9939e10bb4b963c918ce Mon Sep 17 00:00:00 2001 From: Erik Hendrix Date: Thu, 25 Feb 2021 07:34:02 -0700 Subject: [PATCH] Add unknown devices --- example.py | 17 ++++++++++++++++- pymyq/account.py | 36 +++++++++++++++++++++++++----------- pymyq/device.py | 26 +++++++++++++++----------- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/example.py b/example.py index 28b7c02..02b2748 100644 --- a/example.py +++ b/example.py @@ -133,7 +133,7 @@ async def print_gateways(account: MyQAccount): """Print gateways for account Args: - account (MyQAccount): Account for which to gateways + account (MyQAccount): Account for which to retrieve gateways """ print(f" Gateways: {len(account.gateways)}") print(" ------------") @@ -144,6 +144,21 @@ async def print_gateways(account: MyQAccount): print("------------------------------") +async def print_other(account: MyQAccount): + """Print unknown/other devices for account + + Args: + account (MyQAccount): Account for which to retrieve unknown devices + """ + print(f" Other: {len(account.other)}") + print(" ------------") + if len(account.other) != 0: + for idx, device in enumerate(account.other.values()): + print_info(number=idx, device=device) + + print("------------------------------") + + async def main() -> None: """Create the aiohttp session and run the example.""" logging.basicConfig(level=logging.DEBUG) diff --git a/pymyq/account.py b/pymyq/account.py index 1e2ef63..176a806 100644 --- a/pymyq/account.py +++ b/pymyq/account.py @@ -66,7 +66,7 @@ def covers(self) -> Dict[str, MyQGaragedoor]: @property def lamps(self) -> Dict[str, MyQLamp]: - """Return only those devices that are covers.""" + """Return only those devices that are lamps.""" return { device_id: device for device_id, device in self.devices.items() @@ -75,13 +75,23 @@ def lamps(self) -> Dict[str, MyQLamp]: @property def gateways(self) -> Dict[str, MyQDevice]: - """Return only those devices that are covers.""" + """Return only those devices that are gateways.""" return { device_id: device for device_id, device in self.devices.items() if device.device_json["device_family"] == DEVICE_FAMILY_GATEWAY } + @property + def other(self) -> Dict[str, MyQDevice]: + """Return only those devices that are covers.""" + return { + device_id: device + for device_id, device in self.devices.items() + if type(device) is MyQDevice + and device.device_json["device_family"] != DEVICE_FAMILY_GATEWAY + } + async def _get_devices(self) -> None: _LOGGER.debug("Retrieving devices for account %s", self.name or self.id) @@ -136,20 +146,24 @@ async def _get_devices(self) -> None: device_json=device, state_update=state_update_timestmp, ) - elif device.get("device_family") == DEVICE_FAMILY_GATEWAY: - _LOGGER.debug( - "Adding new gateway with serial number %s", serial_number - ) + else: + if device.get("device_family") == DEVICE_FAMILY_GATEWAY: + _LOGGER.debug( + "Adding new gateway with serial number %s", + serial_number, + ) + else: + _LOGGER.debug( + "Adding unknown device family %s with serial number %s", + device.get("device_family"), + serial_number, + ) + new_device = MyQDevice( account=self, device_json=device, state_update=state_update_timestmp, ) - else: - _LOGGER.warning( - "Unknown device family %s", device.get("device_family") - ) - new_device = None if new_device: self._devices[serial_number] = new_device diff --git a/pymyq/device.py b/pymyq/device.py index 9b6a7ef..c3e1257 100644 --- a/pymyq/device.py +++ b/pymyq/device.py @@ -37,24 +37,24 @@ def account(self) -> "MyQAccount": return self._account @property - def device_family(self) -> str: + def device_family(self) -> Optional[str]: """Return the family in which this device lives.""" - return self.device_json["device_family"] + return self.device_json.get("device_family") @property - def device_id(self) -> str: + def device_id(self) -> Optional[str]: """Return the device ID (serial number).""" - return self.device_json["serial_number"] + return self.device_json.get("serial_number") @property - def device_platform(self) -> str: + def device_platform(self) -> Optional[str]: """Return the device platform.""" - return self.device_json["device_platform"] + return self.device_json.get("device_platform") @property - def device_type(self) -> str: + def device_type(self) -> Optional[str]: """Return the device type.""" - return self.device_json[DEVICE_TYPE] + return self.device_json.get(DEVICE_TYPE) @property def firmware_version(self) -> Optional[str]: @@ -62,14 +62,18 @@ def firmware_version(self) -> Optional[str]: return self.device_json["state"].get("firmware_version") @property - def name(self) -> bool: + def name(self) -> Optional[str]: """Return the device name.""" - return self.device_json["name"] + return self.device_json.get("name") @property def online(self) -> bool: """Return whether the device is online.""" - return self.device_json["state"].get("online") is True + state = self.device_json.get("state") + if state is None: + return False + + return state.get("online") is True @property def parent_device_id(self) -> Optional[str]: