mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Add fault binary sensors to tuya dehumidifer (#148485)
This commit is contained in:
parent
058e1ede10
commit
4f27058a68
@ -15,9 +15,10 @@ from homeassistant.const import EntityCategory
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
from homeassistant.util.json import json_loads
|
||||||
|
|
||||||
from . import TuyaConfigEntry
|
from . import TuyaConfigEntry
|
||||||
from .const import TUYA_DISCOVERY_NEW, DPCode
|
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType
|
||||||
from .entity import TuyaEntity
|
from .entity import TuyaEntity
|
||||||
|
|
||||||
|
|
||||||
@ -31,6 +32,9 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|||||||
# Value or values to consider binary sensor to be "on"
|
# Value or values to consider binary sensor to be "on"
|
||||||
on_value: bool | float | int | str | set[bool | float | int | str] = True
|
on_value: bool | float | int | str | set[bool | float | int | str] = True
|
||||||
|
|
||||||
|
# For DPType.BITMAP, the bitmap_key is used to extract the bit mask
|
||||||
|
bitmap_key: str | None = None
|
||||||
|
|
||||||
|
|
||||||
# Commonly used sensors
|
# Commonly used sensors
|
||||||
TAMPER_BINARY_SENSOR = TuyaBinarySensorEntityDescription(
|
TAMPER_BINARY_SENSOR = TuyaBinarySensorEntityDescription(
|
||||||
@ -71,6 +75,34 @@ BINARY_SENSORS: dict[str, tuple[TuyaBinarySensorEntityDescription, ...]] = {
|
|||||||
),
|
),
|
||||||
TAMPER_BINARY_SENSOR,
|
TAMPER_BINARY_SENSOR,
|
||||||
),
|
),
|
||||||
|
# Dehumidifier
|
||||||
|
# https://developer.tuya.com/en/docs/iot/categorycs?id=Kaiuz1vcz4dha
|
||||||
|
"cs": (
|
||||||
|
TuyaBinarySensorEntityDescription(
|
||||||
|
key="tankfull",
|
||||||
|
dpcode=DPCode.FAULT,
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
bitmap_key="tankfull",
|
||||||
|
translation_key="tankfull",
|
||||||
|
),
|
||||||
|
TuyaBinarySensorEntityDescription(
|
||||||
|
key="defrost",
|
||||||
|
dpcode=DPCode.FAULT,
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
bitmap_key="defrost",
|
||||||
|
translation_key="defrost",
|
||||||
|
),
|
||||||
|
TuyaBinarySensorEntityDescription(
|
||||||
|
key="wet",
|
||||||
|
dpcode=DPCode.FAULT,
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
bitmap_key="wet",
|
||||||
|
translation_key="wet",
|
||||||
|
),
|
||||||
|
),
|
||||||
# Smart Pet Feeder
|
# Smart Pet Feeder
|
||||||
# https://developer.tuya.com/en/docs/iot/categorycwwsq?id=Kaiuz2b6vydld
|
# https://developer.tuya.com/en/docs/iot/categorycwwsq?id=Kaiuz2b6vydld
|
||||||
"cwwsq": (
|
"cwwsq": (
|
||||||
@ -343,6 +375,22 @@ BINARY_SENSORS: dict[str, tuple[TuyaBinarySensorEntityDescription, ...]] = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _get_bitmap_bit_mask(
|
||||||
|
device: CustomerDevice, dpcode: str, bitmap_key: str | None
|
||||||
|
) -> int | None:
|
||||||
|
"""Get the bit mask for a given bitmap description."""
|
||||||
|
if (
|
||||||
|
bitmap_key is None
|
||||||
|
or (status_range := device.status_range.get(dpcode)) is None
|
||||||
|
or status_range.type != DPType.BITMAP
|
||||||
|
or not isinstance(bitmap_values := json_loads(status_range.values), dict)
|
||||||
|
or not isinstance(bitmap_labels := bitmap_values.get("label"), list)
|
||||||
|
or bitmap_key not in bitmap_labels
|
||||||
|
):
|
||||||
|
return None
|
||||||
|
return bitmap_labels.index(bitmap_key)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: TuyaConfigEntry,
|
entry: TuyaConfigEntry,
|
||||||
@ -361,12 +409,23 @@ async def async_setup_entry(
|
|||||||
for description in descriptions:
|
for description in descriptions:
|
||||||
dpcode = description.dpcode or description.key
|
dpcode = description.dpcode or description.key
|
||||||
if dpcode in device.status:
|
if dpcode in device.status:
|
||||||
entities.append(
|
mask = _get_bitmap_bit_mask(
|
||||||
TuyaBinarySensorEntity(
|
device, dpcode, description.bitmap_key
|
||||||
device, hass_data.manager, description
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
description.bitmap_key is None # Regular binary sensor
|
||||||
|
or mask is not None # Bitmap sensor with valid mask
|
||||||
|
):
|
||||||
|
entities.append(
|
||||||
|
TuyaBinarySensorEntity(
|
||||||
|
device,
|
||||||
|
hass_data.manager,
|
||||||
|
description,
|
||||||
|
mask,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
async_discover_device([*hass_data.manager.device_map])
|
async_discover_device([*hass_data.manager.device_map])
|
||||||
@ -386,11 +445,13 @@ class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity):
|
|||||||
device: CustomerDevice,
|
device: CustomerDevice,
|
||||||
device_manager: Manager,
|
device_manager: Manager,
|
||||||
description: TuyaBinarySensorEntityDescription,
|
description: TuyaBinarySensorEntityDescription,
|
||||||
|
bit_mask: int | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Init Tuya binary sensor."""
|
"""Init Tuya binary sensor."""
|
||||||
super().__init__(device, device_manager)
|
super().__init__(device, device_manager)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_unique_id = f"{super().unique_id}{description.key}"
|
self._attr_unique_id = f"{super().unique_id}{description.key}"
|
||||||
|
self._bit_mask = bit_mask
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
@ -399,6 +460,10 @@ class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity):
|
|||||||
if dpcode not in self.device.status:
|
if dpcode not in self.device.status:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if self._bit_mask is not None:
|
||||||
|
# For bitmap sensors, check the specific bit mask
|
||||||
|
return (self.device.status[dpcode] & (1 << self._bit_mask)) != 0
|
||||||
|
|
||||||
if isinstance(self.entity_description.on_value, set):
|
if isinstance(self.entity_description.on_value, set):
|
||||||
return self.device.status[dpcode] in self.entity_description.on_value
|
return self.device.status[dpcode] in self.entity_description.on_value
|
||||||
|
|
||||||
|
@ -82,6 +82,7 @@ class WorkMode(StrEnum):
|
|||||||
class DPType(StrEnum):
|
class DPType(StrEnum):
|
||||||
"""Data point types."""
|
"""Data point types."""
|
||||||
|
|
||||||
|
BITMAP = "Bitmap"
|
||||||
BOOLEAN = "Boolean"
|
BOOLEAN = "Boolean"
|
||||||
ENUM = "Enum"
|
ENUM = "Enum"
|
||||||
INTEGER = "Integer"
|
INTEGER = "Integer"
|
||||||
|
@ -14,8 +14,7 @@ from .const import DOMAIN, LOGGER, TUYA_HA_SIGNAL_UPDATE_ENTITY, DPCode, DPType
|
|||||||
from .models import EnumTypeData, IntegerTypeData
|
from .models import EnumTypeData, IntegerTypeData
|
||||||
|
|
||||||
_DPTYPE_MAPPING: dict[str, DPType] = {
|
_DPTYPE_MAPPING: dict[str, DPType] = {
|
||||||
"Bitmap": DPType.RAW,
|
"bitmap": DPType.BITMAP,
|
||||||
"bitmap": DPType.RAW,
|
|
||||||
"bool": DPType.BOOLEAN,
|
"bool": DPType.BOOLEAN,
|
||||||
"enum": DPType.ENUM,
|
"enum": DPType.ENUM,
|
||||||
"json": DPType.JSON,
|
"json": DPType.JSON,
|
||||||
|
@ -56,6 +56,15 @@
|
|||||||
},
|
},
|
||||||
"tilt": {
|
"tilt": {
|
||||||
"name": "Tilt"
|
"name": "Tilt"
|
||||||
|
},
|
||||||
|
"tankfull": {
|
||||||
|
"name": "Tank full"
|
||||||
|
},
|
||||||
|
"defrost": {
|
||||||
|
"name": "Defrost"
|
||||||
|
},
|
||||||
|
"wet": {
|
||||||
|
"name": "Wet"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"button": {
|
"button": {
|
||||||
|
@ -19,6 +19,7 @@ DEVICE_MOCKS = {
|
|||||||
Platform.LIGHT,
|
Platform.LIGHT,
|
||||||
],
|
],
|
||||||
"cs_arete_two_12l_dehumidifier_air_purifier": [
|
"cs_arete_two_12l_dehumidifier_air_purifier": [
|
||||||
|
Platform.BINARY_SENSOR,
|
||||||
Platform.FAN,
|
Platform.FAN,
|
||||||
Platform.HUMIDIFIER,
|
Platform.HUMIDIFIER,
|
||||||
Platform.SELECT,
|
Platform.SELECT,
|
||||||
|
@ -1,4 +1,151 @@
|
|||||||
# serializer version: 1
|
# serializer version: 1
|
||||||
|
# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][binary_sensor.dehumidifier_defrost-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'binary_sensor',
|
||||||
|
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||||
|
'entity_id': 'binary_sensor.dehumidifier_defrost',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Defrost',
|
||||||
|
'platform': 'tuya',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'defrost',
|
||||||
|
'unique_id': 'tuya.bf3fce6af592f12df3gbgqdefrost',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][binary_sensor.dehumidifier_defrost-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'problem',
|
||||||
|
'friendly_name': 'Dehumidifier Defrost',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.dehumidifier_defrost',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'off',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][binary_sensor.dehumidifier_tank_full-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'binary_sensor',
|
||||||
|
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||||
|
'entity_id': 'binary_sensor.dehumidifier_tank_full',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Tank full',
|
||||||
|
'platform': 'tuya',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'tankfull',
|
||||||
|
'unique_id': 'tuya.bf3fce6af592f12df3gbgqtankfull',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][binary_sensor.dehumidifier_tank_full-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'problem',
|
||||||
|
'friendly_name': 'Dehumidifier Tank full',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.dehumidifier_tank_full',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'off',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][binary_sensor.dehumidifier_wet-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'binary_sensor',
|
||||||
|
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||||
|
'entity_id': 'binary_sensor.dehumidifier_wet',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Wet',
|
||||||
|
'platform': 'tuya',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'wet',
|
||||||
|
'unique_id': 'tuya.bf3fce6af592f12df3gbgqwet',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][binary_sensor.dehumidifier_wet-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'problem',
|
||||||
|
'friendly_name': 'Dehumidifier Wet',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.dehumidifier_wet',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'off',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
# name: test_platform_setup_and_discovery[mcs_door_sensor][binary_sensor.door_garage_door-entry]
|
# name: test_platform_setup_and_discovery[mcs_door_sensor][binary_sensor.door_garage_door-entry]
|
||||||
EntityRegistryEntrySnapshot({
|
EntityRegistryEntrySnapshot({
|
||||||
'aliases': set({
|
'aliases': set({
|
||||||
|
@ -56,3 +56,38 @@ async def test_platform_setup_no_discovery(
|
|||||||
assert not er.async_entries_for_config_entry(
|
assert not er.async_entries_for_config_entry(
|
||||||
entity_registry, mock_config_entry.entry_id
|
entity_registry, mock_config_entry.entry_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"mock_device_code",
|
||||||
|
["cs_arete_two_12l_dehumidifier_air_purifier"],
|
||||||
|
)
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("fault_value", "tankfull", "defrost", "wet"),
|
||||||
|
[
|
||||||
|
(0, "off", "off", "off"),
|
||||||
|
(0x1, "on", "off", "off"),
|
||||||
|
(0x2, "off", "on", "off"),
|
||||||
|
(0x80, "off", "off", "on"),
|
||||||
|
(0x83, "on", "on", "on"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.BINARY_SENSOR])
|
||||||
|
async def test_bitmap(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_manager: ManagerCompat,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_device: CustomerDevice,
|
||||||
|
fault_value: int,
|
||||||
|
tankfull: str,
|
||||||
|
defrost: str,
|
||||||
|
wet: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test BITMAP fault sensor on cs_arete_two_12l_dehumidifier_air_purifier."""
|
||||||
|
mock_device.status["fault"] = fault_value
|
||||||
|
|
||||||
|
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
|
||||||
|
|
||||||
|
assert hass.states.get("binary_sensor.dehumidifier_tank_full").state == tankfull
|
||||||
|
assert hass.states.get("binary_sensor.dehumidifier_defrost").state == defrost
|
||||||
|
assert hass.states.get("binary_sensor.dehumidifier_wet").state == wet
|
||||||
|
Loading…
x
Reference in New Issue
Block a user