Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions roborock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from .roborock_message import (
RoborockMessage,
)
from .roborock_typing import RoborockCommand
from .util import get_next_int, get_running_loop_or_create_one

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -124,11 +123,3 @@ def _async_response(self, request_id: int, protocol_id: int = 0) -> Any:
@abstractmethod
async def send_message(self, roborock_message: RoborockMessage):
"""Send a message to the Roborock device."""

@abstractmethod
async def _send_command(
self,
method: RoborockCommand | str,
params: list | dict | int | None = None,
):
"""Send a command to the Roborock device."""
30 changes: 21 additions & 9 deletions roborock/version_1_apis/roborock_client_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math
import struct
import time
from abc import ABC
from abc import ABC, abstractmethod
from collections.abc import Callable, Coroutine
from typing import Any, TypeVar, final

Expand Down Expand Up @@ -78,12 +78,15 @@
EVICT_TIME = 60


_SendCommandT = Callable[[RoborockCommand | str, list | dict | int | None], Any]


class AttributeCache:
def __init__(self, attribute: RoborockAttribute, api: RoborockClient):
def __init__(self, attribute: RoborockAttribute, loop: asyncio.AbstractEventLoop, send_command: _SendCommandT):
self.attribute = attribute
self.api = api
self._send_command = send_command
self.attribute = attribute
self.task = RepeatableTask(self.api.event_loop, self._async_value, EVICT_TIME)
self.task = RepeatableTask(loop, self._async_value, EVICT_TIME)
self._value: Any = None
self._mutex = asyncio.Lock()
self.unsupported: bool = False
Expand All @@ -96,7 +99,7 @@ async def _async_value(self):
if self.unsupported:
return None
try:
self._value = await self.api._send_command(self.attribute.get_command)
self._value = await self._send_command(self.attribute.get_command, None)
except UnknownMethodError as err:
# Limit the amount of times we call unsupported methods
self.unsupported = True
Expand All @@ -115,21 +118,21 @@ def stop(self):
async def update_value(self, params) -> None:
if self.attribute.set_command is None:
raise RoborockException(f"{self.attribute.attribute} have no set command")
response = await self.api._send_command(self.attribute.set_command, params)
response = await self._send_command(self.attribute.set_command, params)
await self._async_value()
return response

async def add_value(self, params):
if self.attribute.add_command is None:
raise RoborockException(f"{self.attribute.attribute} have no add command")
response = await self.api._send_command(self.attribute.add_command, params)
response = await self._send_command(self.attribute.add_command, params)
await self._async_value()
return response

async def close_value(self, params=None) -> None:
if self.attribute.close_command is None:
raise RoborockException(f"{self.attribute.attribute} have no close command")
response = await self.api._send_command(self.attribute.close_command, params)
response = await self._send_command(self.attribute.close_command, params)
await self._async_value()
return response

Expand All @@ -153,7 +156,8 @@ def __init__(self, device_info: DeviceData, endpoint: str):
super().__init__(device_info)
self._status_type: type[Status] = ModelStatus.get(device_info.model, S7MaxVStatus)
self.cache: dict[CacheableAttribute, AttributeCache] = {
cacheable_attribute: AttributeCache(attr, self) for cacheable_attribute, attr in get_cache_map().items()
cacheable_attribute: AttributeCache(attr, self.event_loop, self._send_command)
for cacheable_attribute, attr in get_cache_map().items()
}
if device_info.device.duid not in self._listeners:
self._listeners[device_info.device.duid] = ListenerModel({}, self.cache)
Expand Down Expand Up @@ -364,6 +368,14 @@ def _get_payload(
)
return request_id, timestamp, payload

@abstractmethod
async def _send_command(
self,
method: RoborockCommand | str,
params: list | dict | int | None = None,
) -> Any:
"""Send a command to the Roborock device."""

def on_message_received(self, messages: list[RoborockMessage]) -> None:
try:
self._last_device_msg_in = time.monotonic()
Expand Down
4 changes: 3 additions & 1 deletion roborock/version_a01_apis/roborock_client_a01.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
payload = message.payload
try:
payload = unpad(payload, AES.block_size)
except Exception:
except Exception as err:
self._logger.debug("Failed to unpad payload: %s", err)
continue
payload_json = json.loads(payload.decode())
for data_point_number, data_point in payload_json.get("dps").items():
self._logger.debug("data point number=%s", data_point_number)
data_point_protocol: RoborockDyadDataProtocol | RoborockZeoProtocol
entries: dict
if self.category == RoborockCategory.WET_DRY_VAC:
Expand Down
Loading
Loading