Skip to content

Commit

Permalink
Add find_device_by_identifier (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
inverse authored Oct 7, 2021
1 parent 0c856a6 commit 1e74a08
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 0.2.0 - 2021-10-07

### Added

- Add `find_device_by_identifier` to discovered

## 0.1.1 - 2021-10-07

### Changed
Expand Down
16 changes: 16 additions & 0 deletions iolite_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ def find_room_by_name(self, name: str) -> Optional[Room]:
"""
return self._find_room_by_attribute_value("name", name)

def find_device_by_identifier(self, identifier: str) -> Optional[Device]:
"""Find a device by identifier.
:param identifier: The identifier of the device
:return: The matched device or None
"""
for room in self.discovered_rooms.values():
if room.devices.get(identifier):
return room.devices.get(identifier)

for devices in self.unmapped_entities.values():
for device in devices:
if device.identifier == identifier:
return device

return None

def _find_room_by_attribute_value(
self, attribute: str, value: str
) -> Optional[Room]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "iolite-client"
version = "0.1.1"
version = "0.2.0"
description = "API client for interacting with IOLite's remote API"
authors = ["Malachi Soord <[email protected]>"]
license = "MIT"
Expand Down
21 changes: 21 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ def test_add_heating_unmapped(self):
self.assertTrue(len(self.discovered.unmapped_entities) == 0)
self.assertEqual(self.bedroom_heating, self.discovered.get_rooms()[0].heating)

def test_find_device_by_identifier(self):
self.discovered.add_room(self.bedroom)
self.discovered.add_device(self.bedroom_switch)
self.assertEqual(
self.bedroom_switch,
self.discovered.find_device_by_identifier(self.bedroom_switch.identifier),
)

def test_find_device_by_identifier_unmapped(self):
self.discovered.add_device(self.bedroom_switch)
self.assertEqual(
self.bedroom_switch,
self.discovered.find_device_by_identifier(self.bedroom_switch.identifier),
)

def test_find_device_by_identifier_not_found(self):
self.assertEqual(
None,
self.discovered.find_device_by_identifier(self.bedroom_switch.identifier),
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 1e74a08

Please sign in to comment.