Optimise Upnp event handling in SamsungTV (#68828)

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/68360/head^2
epenet 2022-03-29 10:55:05 +02:00 committed by GitHub
parent 2e9c89024b
commit d4ab48a049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 7 deletions

View File

@ -230,14 +230,28 @@ class SamsungTVDevice(MediaPlayerEntity):
self._update_from_upnp()
@callback
def _update_from_upnp(self) -> None:
def _update_from_upnp(self) -> bool:
# Upnp events can affect other attributes that we currently do not track
# We want to avoid checking every attribute in 'async_write_ha_state' as we
# currently only care about two attributes
if (dmr_device := self._dmr_device) is None:
return
return False
if (volume_level := dmr_device.volume_level) is not None:
has_updates = False
if (
volume_level := dmr_device.volume_level
) is not None and self._attr_volume_level != volume_level:
self._attr_volume_level = volume_level
if (is_muted := dmr_device.is_volume_muted) is not None:
has_updates = True
if (
is_muted := dmr_device.is_volume_muted
) is not None and self._attr_is_volume_muted != is_muted:
self._attr_is_volume_muted = is_muted
has_updates = True
return has_updates
async def _async_startup_app_list(self) -> None:
await self._bridge.async_request_app_list()
@ -311,9 +325,8 @@ class SamsungTVDevice(MediaPlayerEntity):
self, service: UpnpService, state_variables: Sequence[UpnpStateVariable]
) -> None:
"""State variable(s) changed, let home-assistant know."""
self._update_from_upnp()
self.async_write_ha_state()
if self._update_from_upnp():
self.async_write_ha_state()
async def _async_launch_app(self, app_id: str) -> None:
"""Send launch_app to the tv."""