roborock.devices.traits.v1.network_info

Trait for device network information.

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

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.

NetworkInfoTrait(device_uid: str, device_cache: roborock.devices.cache.DeviceCache)
28    def __init__(self, device_uid: str, device_cache: DeviceCache) -> None:  # pylint: disable=super-init-not-called
29        """Initialize the trait."""
30        self._device_uid = device_uid
31        self._device_cache = device_cache
32        self.ip = ""

Initialize the trait.

command = <RoborockCommand.GET_NETWORK_INFO: 'get_network_info'>
ip
async def refresh(self) -> None:
34    async def refresh(self) -> None:
35        """Refresh the network info from the cache."""
36
37        device_cache_data = await self._device_cache.get()
38        if device_cache_data.network_info:
39            _LOGGER.debug("Using cached network info for device %s", self._device_uid)
40            self._update_trait_values(device_cache_data.network_info)
41            return
42
43        # Load from device if not in cache
44        _LOGGER.debug("No cached network info for device %s, fetching from device", self._device_uid)
45        await super().refresh()
46
47        # Update the cache with the new network info
48        device_cache_data = await self._device_cache.get()
49        device_cache_data.network_info = self
50        await self._device_cache.set(device_cache_data)

Refresh the network info from the cache.