mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Send Roborock commands via cloud api when needed (#138496)
* Send via cloud api when needed * Extract logic to helper function * change to class method
This commit is contained in:
parent
244b666dee
commit
6754bf2466
@ -8,7 +8,11 @@ from roborock.containers import Consumable, Status
|
|||||||
from roborock.exceptions import RoborockException
|
from roborock.exceptions import RoborockException
|
||||||
from roborock.roborock_message import RoborockDataProtocol
|
from roborock.roborock_message import RoborockDataProtocol
|
||||||
from roborock.roborock_typing import RoborockCommand
|
from roborock.roborock_typing import RoborockCommand
|
||||||
from roborock.version_1_apis.roborock_client_v1 import AttributeCache, RoborockClientV1
|
from roborock.version_1_apis.roborock_client_v1 import (
|
||||||
|
CLOUD_REQUIRED,
|
||||||
|
AttributeCache,
|
||||||
|
RoborockClientV1,
|
||||||
|
)
|
||||||
from roborock.version_1_apis.roborock_mqtt_client_v1 import RoborockMqttClientV1
|
from roborock.version_1_apis.roborock_mqtt_client_v1 import RoborockMqttClientV1
|
||||||
from roborock.version_a01_apis import RoborockClientA01
|
from roborock.version_a01_apis import RoborockClientA01
|
||||||
|
|
||||||
@ -53,14 +57,16 @@ class RoborockEntityV1(RoborockEntity):
|
|||||||
"""Get an item from the api cache."""
|
"""Get an item from the api cache."""
|
||||||
return self._api.cache[attribute]
|
return self._api.cache[attribute]
|
||||||
|
|
||||||
async def send(
|
@classmethod
|
||||||
self,
|
async def _send_command(
|
||||||
|
cls,
|
||||||
command: RoborockCommand | str,
|
command: RoborockCommand | str,
|
||||||
|
api: RoborockClientV1,
|
||||||
params: dict[str, Any] | list[Any] | int | None = None,
|
params: dict[str, Any] | list[Any] | int | None = None,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""Send a command to a vacuum cleaner."""
|
"""Send a Roborock command with params to a given api."""
|
||||||
try:
|
try:
|
||||||
response: dict = await self._api.send_command(command, params)
|
response: dict = await api.send_command(command, params)
|
||||||
except RoborockException as err:
|
except RoborockException as err:
|
||||||
if isinstance(command, RoborockCommand):
|
if isinstance(command, RoborockCommand):
|
||||||
command_name = command.name
|
command_name = command.name
|
||||||
@ -75,6 +81,14 @@ class RoborockEntityV1(RoborockEntity):
|
|||||||
) from err
|
) from err
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
async def send(
|
||||||
|
self,
|
||||||
|
command: RoborockCommand | str,
|
||||||
|
params: dict[str, Any] | list[Any] | int | None = None,
|
||||||
|
) -> dict:
|
||||||
|
"""Send a command to a vacuum cleaner."""
|
||||||
|
return await self._send_command(command, self._api, params)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def api(self) -> RoborockClientV1:
|
def api(self) -> RoborockClientV1:
|
||||||
"""Returns the api."""
|
"""Returns the api."""
|
||||||
@ -152,7 +166,10 @@ class RoborockCoordinatedEntityV1(
|
|||||||
params: dict[str, Any] | list[Any] | int | None = None,
|
params: dict[str, Any] | list[Any] | int | None = None,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""Overloads normal send command but refreshes coordinator."""
|
"""Overloads normal send command but refreshes coordinator."""
|
||||||
res = await super().send(command, params)
|
if command in CLOUD_REQUIRED:
|
||||||
|
res = await self._send_command(command, self.coordinator.cloud_api, params)
|
||||||
|
else:
|
||||||
|
res = await self._send_command(command, self._api, params)
|
||||||
await self.coordinator.async_refresh()
|
await self.coordinator.async_refresh()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -117,6 +117,30 @@ async def test_commands(
|
|||||||
assert mock_send_command.call_args[0][1] == called_params
|
assert mock_send_command.call_args[0][1] == called_params
|
||||||
|
|
||||||
|
|
||||||
|
async def test_cloud_command(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
bypass_api_fixture,
|
||||||
|
setup_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test sending commands to the vacuum."""
|
||||||
|
|
||||||
|
vacuum = hass.states.get(ENTITY_ID)
|
||||||
|
assert vacuum
|
||||||
|
|
||||||
|
data = {ATTR_ENTITY_ID: ENTITY_ID, "command": "get_map_v1"}
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.roborock.coordinator.RoborockMqttClientV1.send_command"
|
||||||
|
) as mock_send_command:
|
||||||
|
await hass.services.async_call(
|
||||||
|
Platform.VACUUM,
|
||||||
|
SERVICE_SEND_COMMAND,
|
||||||
|
data,
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
assert mock_send_command.call_count == 1
|
||||||
|
assert mock_send_command.call_args[0][0] == RoborockCommand.GET_MAP_V1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("in_cleaning_int", "expected_command"),
|
("in_cleaning_int", "expected_command"),
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user