Skip to content

Commit 5508c42

Browse files
Handle deleted device (#938)
1 parent 8e0ef76 commit 5508c42

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

custom_components/battery_notes/binary_sensor.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None:
7777
device_registry = dr.async_get(hass)
7878

7979
device_id = entry.data.get(CONF_DEVICE_ID)
80-
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
81-
82-
return device_id
8380

81+
if device_registry.async_get(device_id):
82+
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
83+
return device_id
84+
return None
8485

8586
async def async_setup_entry(
8687
hass: HomeAssistant,
@@ -131,6 +132,9 @@ async def async_registry_updated(event: Event) -> None:
131132

132133
device_id = async_add_to_device(hass, config_entry)
133134

135+
if not device_id:
136+
return
137+
134138
description = BatteryNotesBinarySensorEntityDescription(
135139
unique_id_suffix="_battery_low",
136140
key="_battery_plus_low",

custom_components/battery_notes/button.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None:
6969
device_registry = dr.async_get(hass)
7070

7171
device_id = entry.data.get(CONF_DEVICE_ID)
72-
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
73-
74-
return device_id
7572

73+
if device_registry.async_get(device_id):
74+
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
75+
return device_id
76+
return None
7677

7778
async def async_setup_entry(
7879
hass: HomeAssistant,
@@ -123,6 +124,9 @@ async def async_registry_updated(event: Event) -> None:
123124

124125
device_id = async_add_to_device(hass, config_entry)
125126

127+
if not device_id:
128+
return
129+
126130
enable_replaced = True
127131
if DOMAIN_CONFIG in hass.data[DOMAIN]:
128132
domain_config: dict = hass.data[DOMAIN][DOMAIN_CONFIG]

custom_components/battery_notes/sensor.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None:
107107
device_registry = dr.async_get(hass)
108108

109109
device_id = entry.data.get(CONF_DEVICE_ID)
110-
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
111-
112-
return device_id
113110

111+
if device_registry.async_get(device_id):
112+
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
113+
return device_id
114+
return None
114115

115116
async def async_setup_entry(
116117
hass: HomeAssistant,
@@ -157,6 +158,9 @@ async def async_registry_updated(event: Event) -> None:
157158

158159
device_id = async_add_to_device(hass, config_entry)
159160

161+
if not device_id:
162+
return
163+
160164
device = hass.data[DOMAIN][DATA].devices[config_entry.entry_id]
161165

162166
coordinator = device.coordinator

0 commit comments

Comments
 (0)