Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Roborock sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from roborock.roborock_typing import DeviceProp
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import RoborockConfigEntry
20 from .coordinator import RoborockDataUpdateCoordinator
21 from .entity import RoborockCoordinatedEntityV1
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """A class that describes Roborock binary sensors."""
27 
28  value_fn: Callable[[DeviceProp], bool | int | None]
29 
30 
31 BINARY_SENSOR_DESCRIPTIONS = [
33  key="dry_status",
34  translation_key="mop_drying_status",
35  device_class=BinarySensorDeviceClass.RUNNING,
36  entity_category=EntityCategory.DIAGNOSTIC,
37  value_fn=lambda data: data.status.dry_status,
38  ),
40  key="water_box_carriage_status",
41  translation_key="mop_attached",
42  device_class=BinarySensorDeviceClass.CONNECTIVITY,
43  entity_category=EntityCategory.DIAGNOSTIC,
44  value_fn=lambda data: data.status.water_box_carriage_status,
45  ),
47  key="water_box_status",
48  translation_key="water_box_attached",
49  device_class=BinarySensorDeviceClass.CONNECTIVITY,
50  entity_category=EntityCategory.DIAGNOSTIC,
51  value_fn=lambda data: data.status.water_box_status,
52  ),
54  key="water_shortage",
55  translation_key="water_shortage",
56  device_class=BinarySensorDeviceClass.PROBLEM,
57  entity_category=EntityCategory.DIAGNOSTIC,
58  value_fn=lambda data: data.status.water_shortage_status,
59  ),
61  key="in_cleaning",
62  translation_key="in_cleaning",
63  device_class=BinarySensorDeviceClass.RUNNING,
64  entity_category=EntityCategory.DIAGNOSTIC,
65  value_fn=lambda data: data.status.in_cleaning,
66  ),
67 ]
68 
69 
71  hass: HomeAssistant,
72  config_entry: RoborockConfigEntry,
73  async_add_entities: AddEntitiesCallback,
74 ) -> None:
75  """Set up the Roborock vacuum binary sensors."""
78  coordinator,
79  description,
80  )
81  for coordinator in config_entry.runtime_data.v1
82  for description in BINARY_SENSOR_DESCRIPTIONS
83  if description.value_fn(coordinator.roborock_device_info.props) is not None
84  )
85 
86 
88  """Representation of a Roborock binary sensor."""
89 
90  entity_description: RoborockBinarySensorDescription
91 
92  def __init__(
93  self,
94  coordinator: RoborockDataUpdateCoordinator,
95  description: RoborockBinarySensorDescription,
96  ) -> None:
97  """Initialize the entity."""
98  super().__init__(
99  f"{description.key}_{coordinator.duid_slug}",
100  coordinator,
101  )
102  self.entity_descriptionentity_description = description
103 
104  @property
105  def is_on(self) -> bool:
106  """Return the value reported by the sensor."""
107  return bool(
108  self.entity_descriptionentity_description.value_fn(
109  self.coordinator.roborock_device_info.props
110  )
111  )
None __init__(self, RoborockDataUpdateCoordinator coordinator, RoborockBinarySensorDescription description)
None async_setup_entry(HomeAssistant hass, RoborockConfigEntry config_entry, AddEntitiesCallback async_add_entities)