Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button platform to Roborock #103010

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 116 additions & 0 deletions homeassistant/components/roborock/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""Support for Roborock button."""
from __future__ import annotations

from dataclasses import dataclass

from roborock.roborock_typing import RoborockCommand

from homeassistant.components.button import (
ButtonDeviceClass,
ButtonEntity,
ButtonEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify

from .const import DOMAIN
from .coordinator import RoborockDataUpdateCoordinator
from .device import RoborockEntity


@dataclass
class RoborockButtonDescriptionMixin:
"""Define an entity description mixin for button entities."""

command: RoborockCommand
param: list | dict | None


@dataclass
class RoborockButtonDescription(
ButtonEntityDescription, RoborockButtonDescriptionMixin
):
"""Describes a Roborock button entity."""


CONSUMABLE_BUTTON_DESCRIPTIONS = [
RoborockButtonDescription(
key="reset_sensor_consumable",
device_class=ButtonDeviceClass.UPDATE,
Lash-L marked this conversation as resolved.
Show resolved Hide resolved
translation_key="reset_sensor_consumable",
command=RoborockCommand.RESET_CONSUMABLE,
param=["sensor_dirty_time"],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
RoborockButtonDescription(
key="reset_air_filter_consumable",
device_class=ButtonDeviceClass.UPDATE,
translation_key="reset_air_filter_consumable",
command=RoborockCommand.RESET_CONSUMABLE,
param=["filter_work_time"],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
RoborockButtonDescription(
key="reset_side_brush_consumable",
device_class=ButtonDeviceClass.UPDATE,
translation_key="reset_side_brush_consumable",
command=RoborockCommand.RESET_CONSUMABLE,
param=["side_brush_work_time"],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
RoborockButtonDescription(
key="reset_main_brush_consumable",
device_class=ButtonDeviceClass.UPDATE,
translation_key="reset_main_brush_consumable",
command=RoborockCommand.RESET_CONSUMABLE,
param=["main_brush_work_time"],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
]


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Roborock button platform."""
coordinators: dict[str, RoborockDataUpdateCoordinator] = hass.data[DOMAIN][
config_entry.entry_id
]
async_add_entities(
RoborockButtonEntity(
f"{description.key}_{slugify(device_id)}",
coordinator,
description,
)
for device_id, coordinator in coordinators.items()
for description in CONSUMABLE_BUTTON_DESCRIPTIONS
)


class RoborockButtonEntity(RoborockEntity, ButtonEntity):
"""A class to define Roborock button entities."""

entity_description: RoborockButtonDescription

def __init__(
self,
unique_id: str,
coordinator: RoborockDataUpdateCoordinator,
entity_description: RoborockButtonDescription,
) -> None:
"""Create a button entity."""
super().__init__(unique_id, coordinator.device_info, coordinator.api)
self.entity_description = entity_description

async def async_press(self) -> None:
"""Press the button."""
await self.send(self.entity_description.command, self.entity_description.param)
1 change: 1 addition & 0 deletions homeassistant/components/roborock/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
Platform.TIME,
Platform.NUMBER,
Platform.BINARY_SENSOR,
Platform.BUTTON,
Lash-L marked this conversation as resolved.
Show resolved Hide resolved
]
14 changes: 14 additions & 0 deletions homeassistant/components/roborock/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@
"name": "Water shortage"
}
},
"button": {
"reset_sensor_consumable": {
"name": "Reset sensor consumable"
},
"reset_air_filter_consumable": {
"name": "Reset air filter consumable"
},
"reset_side_brush_consumable": {
"name": "Reset side brush consumable"
},
"reset_main_brush_consumable": {
"name": "Reset main brush consumable"
}
},
"number": {
"volume": {
"name": "Volume"
Expand Down
40 changes: 40 additions & 0 deletions tests/components/roborock/test_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test Roborock Button platform."""
from unittest.mock import patch

import pytest

from homeassistant.components.button import SERVICE_PRESS
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


@pytest.mark.parametrize(
("entity_id"),
[
("button.roborock_s7_maxv_reset_sensor_consumable"),
("button.roborock_s7_maxv_reset_air_filter_consumable"),
("button.roborock_s7_maxv_reset_side_brush_consumable"),
"button.roborock_s7_maxv_reset_main_brush_consumable",
],
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_update_success(
Lash-L marked this conversation as resolved.
Show resolved Hide resolved
hass: HomeAssistant,
bypass_api_fixture,
setup_entry: MockConfigEntry,
entity_id: str,
) -> None:
"""Test allowed changing values for number entities."""
Lash-L marked this conversation as resolved.
Show resolved Hide resolved
# Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None
with patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClient.send_message"
) as mock_send_message:
await hass.services.async_call(
"button",
SERVICE_PRESS,
blocking=True,
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once