Skip to content

Commit

Permalink
Add unknown devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ehendrix23 committed Feb 25, 2021
1 parent 07d3ace commit 672d0a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
17 changes: 16 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ------------")
Expand All @@ -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)
Expand Down
36 changes: 25 additions & 11 deletions pymyq/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions pymyq/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,43 @@ 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]:
"""Return the family in which this device lives."""
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]:
Expand Down

0 comments on commit 672d0a2

Please sign in to comment.