Skip to content

Commit

Permalink
Reintroduce ability to get IP addresses without scope (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
agners authored Jun 11, 2024
1 parent c651e93 commit fe4d83e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions matter_server/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ async def ping_node(self, node_id: int) -> NodePingResult:
async def get_node_ip_addresses(
self, node_id: int, prefer_cache: bool = True, scoped: bool = False
) -> list[str]:
"""Return the currently known (scoped) IP-adress(es)."""
"""Return the currently known (scoped) IP-address(es)."""
if TYPE_CHECKING:
assert self.server_info is not None
if self.server_info.schema_version >= 8:
return cast(
list[str],
await self.send_command(
APICommand.GET_NODE_IP_ADRESSES,
APICommand.GET_NODE_IP_ADDRESSES,
require_schema=8,
node_id=node_id,
prefer_cache=prefer_cache,
Expand Down
2 changes: 1 addition & 1 deletion matter_server/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class APICommand(str, Enum):
READ_ATTRIBUTE = "read_attribute"
WRITE_ATTRIBUTE = "write_attribute"
PING_NODE = "ping_node"
GET_NODE_IP_ADRESSES = "get_node_ip_addresses"
GET_NODE_IP_ADDRESSES = "get_node_ip_addresses"
IMPORT_TEST_NODE = "import_test_node"


Expand Down
25 changes: 15 additions & 10 deletions matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,7 @@ async def _do_ping(ip_address: str) -> None:
node_logger.debug("Pinging address %s", ip_address)
result[ip_address] = await ping_ip(ip_address, timeout, attempts=attempts)

ip_addresses = await self.get_node_ip_addresses(
node_id, prefer_cache=False, scoped=True
)
ip_addresses = await self._get_node_ip_addresses(node_id, prefer_cache=False)
tasks = [_do_ping(x) for x in ip_addresses]
# TODO: replace this gather with a taskgroup once we bump our py version
await asyncio.gather(*tasks)
Expand All @@ -806,14 +804,10 @@ async def _do_ping(ip_address: str) -> None:

return result

@api_command(APICommand.GET_NODE_IP_ADRESSES)
async def get_node_ip_addresses(
self,
node_id: int,
prefer_cache: bool = False,
scoped: bool = False,
async def _get_node_ip_addresses(
self, node_id: int, prefer_cache: bool = False
) -> list[str]:
"""Return the currently known (scoped) IP-address(es)."""
"""Get the IP addresses of a node."""
cached_info = self._last_known_ip_addresses.get(node_id, [])
if prefer_cache and cached_info:
return cached_info
Expand All @@ -839,6 +833,17 @@ async def get_node_ip_addresses(
self._last_known_ip_addresses[node_id] = ip_addresses
return ip_addresses

@api_command(APICommand.GET_NODE_IP_ADDRESSES)
async def get_node_ip_addresses(
self,
node_id: int,
prefer_cache: bool = False,
scoped: bool = False,
) -> list[str]:
"""Return the currently known (scoped) IP-address(es)."""
ip_addresses = await self._get_node_ip_addresses(node_id, prefer_cache)
return ip_addresses if scoped else [x.split("%")[0] for x in ip_addresses]

@api_command(APICommand.IMPORT_TEST_NODE)
async def import_test_node(self, dump: str) -> None:
"""Import test node(s) from a HA or Matter server diagnostics dump."""
Expand Down

0 comments on commit fe4d83e

Please sign in to comment.