From d4ab48a0498bc195a893e9e8a50f4d6fc31e8158 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:55:05 +0200 Subject: [PATCH] Optimise Upnp event handling in SamsungTV (#68828) Co-authored-by: epenet --- .../components/samsungtv/media_player.py | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/samsungtv/media_player.py b/homeassistant/components/samsungtv/media_player.py index 225d99c0cc7..f2dead7bf50 100644 --- a/homeassistant/components/samsungtv/media_player.py +++ b/homeassistant/components/samsungtv/media_player.py @@ -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."""