Catch unexpected battery update payloads on Sonos (#52040)

pull/52049/head
jjlawren 2021-06-20 23:38:07 -05:00 committed by GitHub
parent 067b5258c6
commit 7b5ed8faa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -407,6 +407,14 @@ class SonosSpeaker:
"""Update device properties from an event.""" """Update device properties from an event."""
if more_info := event.variables.get("more_info"): if more_info := event.variables.get("more_info"):
battery_dict = dict(x.split(":") for x in more_info.split(",")) battery_dict = dict(x.split(":") for x in more_info.split(","))
if "BattChg" not in battery_dict:
_LOGGER.debug(
"Unknown device properties update for %s (%s), please report an issue: '%s'",
self.zone_name,
self.model_name,
more_info,
)
return
await self.async_update_battery_info(battery_dict) await self.async_update_battery_info(battery_dict)
self.async_write_entity_states() self.async_write_entity_states()

View File

@ -83,3 +83,23 @@ async def test_battery_on_S1(hass, config_entry, config, soco, battery_event):
power_state = hass.states.get(power.entity_id) power_state = hass.states.get(power.entity_id)
assert power_state.state == STATE_OFF assert power_state.state == STATE_OFF
assert power_state.attributes.get(ATTR_BATTERY_POWER_SOURCE) == "BATTERY" assert power_state.attributes.get(ATTR_BATTERY_POWER_SOURCE) == "BATTERY"
async def test_device_payload_without_battery(
hass, config_entry, config, soco, battery_event, caplog
):
"""Test device properties event update without battery info."""
soco.get_battery_info.return_value = None
await setup_platform(hass, config_entry, config)
subscription = soco.deviceProperties.subscribe.return_value
sub_callback = subscription.callback
bad_payload = "BadKey:BadValue"
battery_event.variables["more_info"] = bad_payload
sub_callback(battery_event)
await hass.async_block_till_done()
assert bad_payload in caplog.text