Repository navigation
Expand file tree
/
Copy pathnetwork_info.py
More file actions
54 lines (40 loc) · 2.05 KB
/
Copy pathnetwork_info.py
File metadata and controls
54 lines (40 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Trait for device network information."""
from __future__ import annotations
import logging
from roborock.data import NetworkInfo
from roborock.devices.cache import Cache
from roborock.devices.traits.v1 import common
from roborock.roborock_typing import RoborockCommand
_LOGGER = logging.getLogger(__name__)
class NetworkInfoTrait(NetworkInfo, common.V1TraitMixin):
"""Trait for device network information.
This trait will always prefer reading from the cache if available. This
information is usually already fetched when creating the device local
connection, so reading from the cache avoids an unnecessary RPC call.
However, we have the fallback to reading from the device if the cache is
not populated for some reason.
"""
command = RoborockCommand.GET_NETWORK_INFO
def __init__(self, device_uid: str, cache: Cache) -> None: # pylint: disable=super-init-not-called
"""Initialize the trait."""
self._device_uid = device_uid
self._cache = cache
self.ip = ""
async def refresh(self) -> None:
"""Refresh the network info from the cache."""
cache_data = await self._cache.get()
if cache_data.network_info and (network_info := cache_data.network_info.get(self._device_uid)):
_LOGGER.debug("Using cached network info for device %s", self._device_uid)
self._update_trait_values(network_info)
return
# Load from device if not in cache
_LOGGER.debug("No cached network info for device %s, fetching from device", self._device_uid)
await super().refresh()
# Update the cache with the new network info
cache_data.network_info[self._device_uid] = self
await self._cache.set(cache_data)
def _parse_response(self, response: common.V1ResponseData) -> NetworkInfo:
"""Parse the response from the device into a NetworkInfo."""
if not isinstance(response, dict):
raise ValueError(f"Unexpected NetworkInfoTrait response format: {response!r}")
return NetworkInfo.from_dict(response)