Skip to content

Fall back to entity_id in mobile-app battery/trigger sensor discovery - #633

Open
neuos wants to merge 1 commit into
gcobb321:masterfrom
neuos:fix/mobapp-battery-sensor-discovery-fallback
Open

neuos wants to merge 1 commit into
gcobb321:masterfrom
neuos:fix/mobapp-battery-sensor-discovery-fallback

Conversation

@neuos

@neuos neuos commented Sep 19, 2026

Copy link
Copy Markdown

Fixes #632

What

_extract_mobile_app_entities() in mobile_app/mobapp_interface.py discovers a mobile-app device's battery/trigger sensors by checking whether the entity's unique_id contains an expected suffix (_battery_level, _battery_state, _last_update_trigger). This one-line change also checks entity_id as a fallback when unique_id doesn't match.

Why

The macOS Home Assistant companion app reports the Mac's internal battery as a labeled power source and embeds the battery's serial number into the sensor's unique_id, so it ends in <serial>_level instead of _battery_level — even though the human-readable entity_id (sensor.<device>_internal_battery_level) still ends with the expected suffix. Discovery misses the sensor entirely for these devices, which (see #632 for the full trace) leads to update_battery_data_from_mobapp() logging an ERROR every poll cycle indefinitely, since Device.mobapp[BATTERY_LEVEL] ends up set to the literal string "sensor." instead of None or a real entity_id.

Every iPhone/iPad in my own setup already has entity_id and unique_id agreeing on this suffix, so this fallback only ever engages for devices where the primary match fails - it doesn't change behavior for anything currently working.

Testing

  • Confirmed the exact failure mode against a live HA instance: Device.mobapp[BATTERY_LEVEL] was "sensor.", update_battery_data_from_mobapp() threw KeyError: 'state' every ~5s (5128 times logged over 7.5h for one device).
  • Applied this same patch to the live install and restarted: the discovery log for that device changed from battery_level or last_update_trigger sensor entities were not found to a clean Mac, Tracked with MobApp Entity: mac_one assigned, and the repeating ERROR stopped entirely (0 occurrences since).
  • python -m py_compile on the changed file.

No CONTRIBUTING.md, issue template, or PR template exists in this repo (checked .github/, repo root, and common legacy paths), so this follows the general commit-message/PR conventions visible in the existing history.

_extract_mobile_app_entities() matches mobile_app sensors by checking
whether the entity's unique_id contains a suffix like '_battery_level'.
That holds for iOS devices, but macOS's companion app reports the
internal battery as a labeled power source and embeds a serial number
in the unique_id instead (e.g. unique_id ending in
'<serial>_level' rather than '_battery_level'), even though the
human-readable entity_id (sensor.<device>_internal_battery_level)
still carries the expected suffix.

When discovery misses the entity this way, start_ic3.py builds
Device.mobapp[BATTERY_LEVEL] as the literal string "sensor." (the
dict lookup falls back to an empty string), which is not None, so the
Gb.conf_data_source_MOBAPP/self.mobapp.get(BATTERY_LEVEL) is None
guard in update_battery_data_from_mobapp() doesn't skip it.
entity_io.get_attributes("sensor.") then hits hass.states.get()
returning None, raises AttributeError, and the caller's own error
handling logs an ERROR every poll cycle (as often as every 5s)
indefinitely for that device.

Falling back to entity_id when unique_id doesn't match fixes discovery
for these devices without changing behavior for anything already
working, since the working iOS entity_ids already contain the same
suffix their unique_id does.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mac companion-app battery sensor not discovered, causes repeating ERROR log every poll

1 participant