11"""Provides a binary sensor for Home Connect."""
22
33from dataclasses import dataclass
4- import logging
4+ from typing import cast
5+
6+ from aiohomeconnect .model import StatusKey
57
68from homeassistant .components .automation import automations_with_entity
79from homeassistant .components .binary_sensor import (
1921 async_delete_issue ,
2022)
2123
22- from . import HomeConnectConfigEntry
23- from .api import HomeConnectDevice
2424from .const import (
25- ATTR_VALUE ,
26- BSH_DOOR_STATE ,
2725 BSH_DOOR_STATE_CLOSED ,
2826 BSH_DOOR_STATE_LOCKED ,
2927 BSH_DOOR_STATE_OPEN ,
30- BSH_REMOTE_CONTROL_ACTIVATION_STATE ,
31- BSH_REMOTE_START_ALLOWANCE_STATE ,
3228 DOMAIN ,
33- REFRIGERATION_STATUS_DOOR_CHILLER ,
3429 REFRIGERATION_STATUS_DOOR_CLOSED ,
35- REFRIGERATION_STATUS_DOOR_FREEZER ,
3630 REFRIGERATION_STATUS_DOOR_OPEN ,
37- REFRIGERATION_STATUS_DOOR_REFRIGERATOR ,
31+ )
32+ from .coordinator import (
33+ HomeConnectApplianceData ,
34+ HomeConnectConfigEntry ,
35+ HomeConnectCoordinator ,
3836)
3937from .entity import HomeConnectEntity
4038
41- _LOGGER = logging .getLogger (__name__ )
4239REFRIGERATION_DOOR_BOOLEAN_MAP = {
4340 REFRIGERATION_STATUS_DOOR_CLOSED : False ,
4441 REFRIGERATION_STATUS_DOOR_OPEN : True ,
@@ -54,19 +51,19 @@ class HomeConnectBinarySensorEntityDescription(BinarySensorEntityDescription):
5451
5552BINARY_SENSORS = (
5653 HomeConnectBinarySensorEntityDescription (
57- key = BSH_REMOTE_CONTROL_ACTIVATION_STATE ,
54+ key = StatusKey . BSH_COMMON_REMOTE_CONTROL_ACTIVE ,
5855 translation_key = "remote_control" ,
5956 ),
6057 HomeConnectBinarySensorEntityDescription (
61- key = BSH_REMOTE_START_ALLOWANCE_STATE ,
58+ key = StatusKey . BSH_COMMON_REMOTE_CONTROL_START_ALLOWED ,
6259 translation_key = "remote_start" ,
6360 ),
6461 HomeConnectBinarySensorEntityDescription (
65- key = "BSH.Common.Status.LocalControlActive" ,
62+ key = StatusKey . BSH_COMMON_LOCAL_CONTROL_ACTIVE ,
6663 translation_key = "local_control" ,
6764 ),
6865 HomeConnectBinarySensorEntityDescription (
69- key = "BSH.Common.Status.BatteryChargingState" ,
66+ key = StatusKey . BSH_COMMON_BATTERY_CHARGING_STATE ,
7067 device_class = BinarySensorDeviceClass .BATTERY_CHARGING ,
7168 boolean_map = {
7269 "BSH.Common.EnumType.BatteryChargingState.Charging" : True ,
@@ -75,7 +72,7 @@ class HomeConnectBinarySensorEntityDescription(BinarySensorEntityDescription):
7572 translation_key = "battery_charging_state" ,
7673 ),
7774 HomeConnectBinarySensorEntityDescription (
78- key = "BSH.Common.Status.ChargingConnection" ,
75+ key = StatusKey . BSH_COMMON_CHARGING_CONNECTION ,
7976 device_class = BinarySensorDeviceClass .PLUG ,
8077 boolean_map = {
8178 "BSH.Common.EnumType.ChargingConnection.Connected" : True ,
@@ -84,31 +81,31 @@ class HomeConnectBinarySensorEntityDescription(BinarySensorEntityDescription):
8481 translation_key = "charging_connection" ,
8582 ),
8683 HomeConnectBinarySensorEntityDescription (
87- key = "ConsumerProducts.CleaningRobot.Status.DustBoxInserted" ,
84+ key = StatusKey . CONSUMER_PRODUCTS_CLEANING_ROBOT_DUST_BOX_INSERTED ,
8885 translation_key = "dust_box_inserted" ,
8986 ),
9087 HomeConnectBinarySensorEntityDescription (
91- key = "ConsumerProducts.CleaningRobot.Status.Lifted" ,
88+ key = StatusKey . CONSUMER_PRODUCTS_CLEANING_ROBOT_LIFTED ,
9289 translation_key = "lifted" ,
9390 ),
9491 HomeConnectBinarySensorEntityDescription (
95- key = "ConsumerProducts.CleaningRobot.Status.Lost" ,
92+ key = StatusKey . CONSUMER_PRODUCTS_CLEANING_ROBOT_LOST ,
9693 translation_key = "lost" ,
9794 ),
9895 HomeConnectBinarySensorEntityDescription (
99- key = REFRIGERATION_STATUS_DOOR_CHILLER ,
96+ key = StatusKey . REFRIGERATION_COMMON_DOOR_CHILLER_COMMON ,
10097 boolean_map = REFRIGERATION_DOOR_BOOLEAN_MAP ,
10198 device_class = BinarySensorDeviceClass .DOOR ,
10299 translation_key = "chiller_door" ,
103100 ),
104101 HomeConnectBinarySensorEntityDescription (
105- key = REFRIGERATION_STATUS_DOOR_FREEZER ,
102+ key = StatusKey . REFRIGERATION_COMMON_DOOR_FREEZER ,
106103 boolean_map = REFRIGERATION_DOOR_BOOLEAN_MAP ,
107104 device_class = BinarySensorDeviceClass .DOOR ,
108105 translation_key = "freezer_door" ,
109106 ),
110107 HomeConnectBinarySensorEntityDescription (
111- key = REFRIGERATION_STATUS_DOOR_REFRIGERATOR ,
108+ key = StatusKey . REFRIGERATION_COMMON_DOOR_REFRIGERATOR ,
112109 boolean_map = REFRIGERATION_DOOR_BOOLEAN_MAP ,
113110 device_class = BinarySensorDeviceClass .DOOR ,
114111 translation_key = "refrigerator_door" ,
@@ -123,45 +120,33 @@ async def async_setup_entry(
123120) -> None :
124121 """Set up the Home Connect binary sensor."""
125122
126- def get_entities () -> list [BinarySensorEntity ]:
127- entities : list [BinarySensorEntity ] = []
128- for device in entry .runtime_data .devices :
129- entities .extend (
130- HomeConnectBinarySensor (device , description )
131- for description in BINARY_SENSORS
132- if description .key in device .appliance .status
133- )
134- if BSH_DOOR_STATE in device .appliance .status :
135- entities .append (HomeConnectDoorBinarySensor (device ))
136- return entities
123+ entities : list [BinarySensorEntity ] = []
124+ for appliance in entry .runtime_data .data .values ():
125+ entities .extend (
126+ HomeConnectBinarySensor (entry .runtime_data , appliance , description )
127+ for description in BINARY_SENSORS
128+ if description .key in appliance .status
129+ )
130+ if StatusKey .BSH_COMMON_DOOR_STATE in appliance .status :
131+ entities .append (HomeConnectDoorBinarySensor (entry .runtime_data , appliance ))
137132
138- async_add_entities (await hass . async_add_executor_job ( get_entities ), True )
133+ async_add_entities (entities )
139134
140135
141136class HomeConnectBinarySensor (HomeConnectEntity , BinarySensorEntity ):
142137 """Binary sensor for Home Connect."""
143138
144139 entity_description : HomeConnectBinarySensorEntityDescription
145140
146- @property
147- def available (self ) -> bool :
148- """Return true if the binary sensor is available."""
149- return self ._attr_is_on is not None
150-
151- async def async_update (self ) -> None :
152- """Update the binary sensor's status."""
153- if not self .device .appliance .status or not (
154- status := self .device .appliance .status .get (self .bsh_key , {}).get (ATTR_VALUE )
155- ):
156- self ._attr_is_on = None
157- return
158- if self .entity_description .boolean_map :
141+ def update_native_value (self ) -> None :
142+ """Set the native value of the binary sensor."""
143+ status = self .appliance .status [cast (StatusKey , self .bsh_key )].value
144+ if isinstance (status , bool ):
145+ self ._attr_is_on = status
146+ elif self .entity_description .boolean_map :
159147 self ._attr_is_on = self .entity_description .boolean_map .get (status )
160- elif status not in [True , False ]:
161- self ._attr_is_on = None
162148 else :
163- self ._attr_is_on = status
164- _LOGGER .debug ("Updated, new state: %s" , self ._attr_is_on )
149+ self ._attr_is_on = None
165150
166151
167152class HomeConnectDoorBinarySensor (HomeConnectBinarySensor ):
@@ -171,13 +156,15 @@ class HomeConnectDoorBinarySensor(HomeConnectBinarySensor):
171156
172157 def __init__ (
173158 self ,
174- device : HomeConnectDevice ,
159+ coordinator : HomeConnectCoordinator ,
160+ appliance : HomeConnectApplianceData ,
175161 ) -> None :
176162 """Initialize the entity."""
177163 super ().__init__ (
178- device ,
164+ coordinator ,
165+ appliance ,
179166 HomeConnectBinarySensorEntityDescription (
180- key = BSH_DOOR_STATE ,
167+ key = StatusKey . BSH_COMMON_DOOR_STATE ,
181168 device_class = BinarySensorDeviceClass .DOOR ,
182169 boolean_map = {
183170 BSH_DOOR_STATE_CLOSED : False ,
@@ -186,8 +173,8 @@ def __init__(
186173 },
187174 ),
188175 )
189- self ._attr_unique_id = f"{ device . appliance .haId } -Door"
190- self ._attr_name = f"{ device . appliance .name } Door"
176+ self ._attr_unique_id = f"{ appliance .info . ha_id } -Door"
177+ self ._attr_name = f"{ appliance . info .name } Door"
191178
192179 async def async_added_to_hass (self ) -> None :
193180 """Call when entity is added to hass."""
@@ -234,6 +221,7 @@ async def async_added_to_hass(self) -> None:
234221
235222 async def async_will_remove_from_hass (self ) -> None :
236223 """Call when entity will be removed from hass."""
224+ await super ().async_will_remove_from_hass ()
237225 async_delete_issue (
238226 self .hass , DOMAIN , f"deprecated_binary_common_door_sensor_{ self .entity_id } "
239227 )
0 commit comments