1818from homeassistant .const import EntityCategory
1919from homeassistant .core import HomeAssistant
2020from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
21+ import homeassistant .helpers .entity_registry as er
2122
23+ from .const import _LOGGER , DOMAIN
2224from .coordinator import AmazonConfigEntry
2325from .entity import AmazonEntity
2426from .utils import async_update_unique_id
@@ -51,11 +53,47 @@ class AmazonBinarySensorEntityDescription(BinarySensorEntityDescription):
5153 ),
5254 is_supported = lambda device , key : device .sensors .get (key ) is not None ,
5355 is_available_fn = lambda device , key : (
54- device .online and device .sensors [key ].error is False
56+ device .online
57+ and (sensor := device .sensors .get (key )) is not None
58+ and sensor .error is False
5559 ),
5660 ),
5761)
5862
63+ DEPRECATED_BINARY_SENSORS : Final = (
64+ AmazonBinarySensorEntityDescription (
65+ key = "bluetooth" ,
66+ entity_category = EntityCategory .DIAGNOSTIC ,
67+ translation_key = "bluetooth" ,
68+ is_on_fn = lambda device , key : False ,
69+ ),
70+ AmazonBinarySensorEntityDescription (
71+ key = "babyCryDetectionState" ,
72+ translation_key = "baby_cry_detection" ,
73+ is_on_fn = lambda device , key : False ,
74+ ),
75+ AmazonBinarySensorEntityDescription (
76+ key = "beepingApplianceDetectionState" ,
77+ translation_key = "beeping_appliance_detection" ,
78+ is_on_fn = lambda device , key : False ,
79+ ),
80+ AmazonBinarySensorEntityDescription (
81+ key = "coughDetectionState" ,
82+ translation_key = "cough_detection" ,
83+ is_on_fn = lambda device , key : False ,
84+ ),
85+ AmazonBinarySensorEntityDescription (
86+ key = "dogBarkDetectionState" ,
87+ translation_key = "dog_bark_detection" ,
88+ is_on_fn = lambda device , key : False ,
89+ ),
90+ AmazonBinarySensorEntityDescription (
91+ key = "waterSoundsDetectionState" ,
92+ translation_key = "water_sounds_detection" ,
93+ is_on_fn = lambda device , key : False ,
94+ ),
95+ )
96+
5997
6098async def async_setup_entry (
6199 hass : HomeAssistant ,
@@ -66,6 +104,8 @@ async def async_setup_entry(
66104
67105 coordinator = entry .runtime_data
68106
107+ entity_registry = er .async_get (hass )
108+
69109 # Replace unique id for "detectionState" binary sensor
70110 await async_update_unique_id (
71111 hass ,
@@ -75,6 +115,16 @@ async def async_setup_entry(
75115 "detectionState" ,
76116 )
77117
118+ # Clean up deprecated sensors
119+ for sensor_desc in DEPRECATED_BINARY_SENSORS :
120+ for serial_num in coordinator .data :
121+ unique_id = f"{ serial_num } -{ sensor_desc .key } "
122+ if entity_id := entity_registry .async_get_entity_id (
123+ BINARY_SENSOR_DOMAIN , DOMAIN , unique_id
124+ ):
125+ _LOGGER .debug ("Removing deprecated entity %s" , entity_id )
126+ entity_registry .async_remove (entity_id )
127+
78128 known_devices : set [str ] = set ()
79129
80130 def _check_device () -> None :
0 commit comments