Skip to content

Commit 4ee839c

Browse files
Fix remove battery note
1 parent dcbf48c commit 4ee839c

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

custom_components/battery_notes/__init__.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,35 +175,34 @@ async def async_remove_entry(hass: HomeAssistant, config_entry: BatteryNotesConf
175175
# Remove any issues raised
176176
ir.async_delete_issue(hass, DOMAIN, f"missing_device_{config_entry.entry_id}")
177177

178-
if not config_entry.runtime_data.coordinator or not config_entry.runtime_data.coordinator.device_id:
179-
return
180-
181-
data = {ATTR_REMOVE: True}
178+
store = await async_get_registry(hass)
179+
coordinator = BatteryNotesCoordinator(hass, config_entry)
182180

183-
config_entry.runtime_data.coordinator.async_update_device_config(
184-
device_id=config_entry.runtime_data.coordinator.device_id, data=data
185-
)
181+
if coordinator.source_entity_id:
182+
store.async_delete_entity(coordinator.source_entity_id)
183+
else:
184+
store.async_delete_device(coordinator.device_id)
186185

187-
_LOGGER.debug("Removed Device %s", config_entry.runtime_data.coordinator.device_id)
186+
_LOGGER.debug("Removed battery note %s", config_entry.entry_id)
188187

189188
# Unhide the battery
190189
entity_registry = er.async_get(hass)
191-
if not config_entry.runtime_data.coordinator.wrapped_battery:
190+
if not coordinator.wrapped_battery:
192191
return
193192

194193
if not (
195194
wrapped_battery_entity_entry := entity_registry.async_get(
196-
config_entry.runtime_data.coordinator.wrapped_battery.entity_id
195+
coordinator.wrapped_battery.entity_id
197196
)
198197
):
199198
return
200199

201200
if wrapped_battery_entity_entry.hidden_by == er.RegistryEntryHider.INTEGRATION:
202201
entity_registry.async_update_entity(
203-
config_entry.runtime_data.coordinator.wrapped_battery.entity_id, hidden_by=None
202+
coordinator.wrapped_battery.entity_id, hidden_by=None
204203
)
205204
_LOGGER.debug(
206-
"Unhidden Original Battery for device%s", config_entry.runtime_data.coordinator.device_id
205+
"Unhidden Original Battery for device%s", coordinator.device_id
207206
)
208207

209208

custom_components/battery_notes/config_flow.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,9 @@ async def async_step_entity(
249249
if entity_entry.device_id:
250250
self.data[CONF_DEVICE_ID] = entity_entry.device_id
251251

252-
if (
253-
DOMAIN in self.hass.data
254-
and DATA_LIBRARY_UPDATER in self.hass.data[DOMAIN]
255-
):
256-
library_updater: LibraryUpdater = self.hass.data[DOMAIN][
257-
DATA_LIBRARY_UPDATER
258-
]
259-
260-
if await library_updater.time_to_update_library(1):
261-
await library_updater.get_library_updates(dt_util.utcnow())
252+
library_updater = LibraryUpdater(self.hass)
253+
if await library_updater.time_to_update_library(1):
254+
await library_updater.get_library_updates(dt_util.utcnow())
262255

263256
device_registry = dr.async_get(self.hass)
264257
device_entry = device_registry.async_get(entity_entry.device_id)

custom_components/battery_notes/coordinator.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ def __init__(
146146
self.config_entry.data.get(CONF_BATTERY_LOW_THRESHOLD, 0)
147147
)
148148

149-
if self.battery_low_threshold == 0:
150-
self.battery_low_threshold = self.config_entry.runtime_data.domain_config.default_battery_low_threshold
149+
if hasattr(self.config_entry, "runtime_data"):
150+
if self.battery_low_threshold == 0:
151+
self.battery_low_threshold = self.config_entry.runtime_data.domain_config.default_battery_low_threshold
151152

152153
self.battery_low_template = self.config_entry.data.get(
153154
CONF_BATTERY_LOW_TEMPLATE
@@ -594,6 +595,9 @@ def battery_type_and_quantity(self) -> str:
594595
@property
595596
def last_replaced(self) -> datetime | None:
596597
"""Get the last replaced datetime."""
598+
if not hasattr(self.config_entry, "runtime_data"):
599+
return None
600+
597601
if self.source_entity_id:
598602
entry = self.config_entry.runtime_data.store.async_get_entity(self.source_entity_id)
599603
else:
@@ -610,6 +614,9 @@ def last_replaced(self) -> datetime | None:
610614
@last_replaced.setter
611615
def last_replaced(self, value: datetime):
612616
"""Set the last replaced datetime and store it."""
617+
if not hasattr(self.config_entry, "runtime_data"):
618+
return
619+
613620
entry = {LAST_REPLACED: value}
614621

615622
if self.source_entity_id:
@@ -621,6 +628,9 @@ def last_replaced(self, value: datetime):
621628
def last_reported(self) -> datetime | None:
622629
"""Get the last reported datetime."""
623630

631+
if not hasattr(self.config_entry, "runtime_data"):
632+
return None
633+
624634
if self.source_entity_id:
625635
entry = self.config_entry.runtime_data.store.async_get_entity(self.source_entity_id)
626636
else:
@@ -639,6 +649,10 @@ def last_reported(self) -> datetime | None:
639649
@last_reported.setter
640650
def last_reported(self, value):
641651
"""Set the last reported datetime and store it."""
652+
653+
if not hasattr(self.config_entry, "runtime_data"):
654+
return
655+
642656
entry = {LAST_REPORTED: value}
643657

644658
if self.source_entity_id:
@@ -650,6 +664,8 @@ def last_reported(self, value):
650664
@property
651665
def last_reported_level(self) -> float | None:
652666
"""Get the last reported level."""
667+
if not hasattr(self.config_entry, "runtime_data"):
668+
return None
653669

654670
if self.source_entity_id:
655671
entry = self.config_entry.runtime_data.store.async_get_entity(self.source_entity_id)
@@ -715,6 +731,9 @@ async def _async_update_data(self):
715731
def async_update_device_config(self, device_id: str, data: dict):
716732
"""Conditional create, update or remove device from store."""
717733

734+
if not hasattr(self.config_entry, "runtime_data"):
735+
return
736+
718737
if ATTR_REMOVE in data:
719738
self.config_entry.runtime_data.store.async_delete_device(device_id)
720739
elif self.config_entry.runtime_data.store.async_get_device(device_id):
@@ -725,6 +744,9 @@ def async_update_device_config(self, device_id: str, data: dict):
725744
def async_update_entity_config(self, entity_id: str, data: dict):
726745
"""Conditional create, update or remove entity from store."""
727746

747+
if not hasattr(self.config_entry, "runtime_data"):
748+
return
749+
728750
if ATTR_REMOVE in data:
729751
self.config_entry.runtime_data.store.async_delete_entity(entity_id)
730752
elif self.config_entry.runtime_data.store.async_get_entity(entity_id):

0 commit comments

Comments
 (0)