Skip to content

Commit 7c116a2

Browse files
authored
Merge pull request #38 from FlyingDiver/copilot/add-error-handling-battery-entity
Add error handling when battery entity does not exist
2 parents f4fbfee + 68d5609 commit 7c116a2

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

  • HomeAssistantAgent.indigoPlugin/Contents/Server Plugin

HomeAssistantAgent.indigoPlugin/Contents/Server Plugin/plugin.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,20 @@ def entity_update(self, entity_id, entity, force_update=False):
506506
# Update battery state if needed
507507
if device.id in self.battery_entities:
508508
battery_entity_id = self.battery_entities.get(device.id)
509-
battery_entity_type, battery_entity_name = battery_entity_id.split('.')
510-
try:
511-
battery_entity = self.ha_entity_map[battery_entity_type][battery_entity_name]
512-
except KeyError:
513-
pass
509+
if not battery_entity_id:
510+
self.logger.warning(f"{device.name}: battery entity ID is not set")
514511
else:
515-
if battery_value := battery_entity.get("state"):
516-
update_list.append({'key': 'batteryLevel', 'value': int(battery_value), 'uiValue': f'{battery_value}%'})
512+
battery_entity_type, battery_entity_name = battery_entity_id.split('.')
513+
try:
514+
battery_entity = self.ha_entity_map[battery_entity_type][battery_entity_name]
515+
except KeyError:
516+
self.logger.warning(f"{device.name}: battery entity '{battery_entity_id}' not found in HA entity map")
517+
else:
518+
if battery_value := battery_entity.get("state"):
519+
try:
520+
update_list.append({'key': 'batteryLevel', 'value': int(battery_value), 'uiValue': f'{battery_value}%'})
521+
except ValueError:
522+
self.logger.warning(f"{device.name}: battery entity '{battery_entity_id}' has non-numeric state: {battery_value}")
517523

518524
old_states_list = self.custom_states.get(device.id, list())
519525
if set(old_states_list) != set(new_states_list):

0 commit comments

Comments
 (0)