From 1e8676bf2c19b7ab7703de110539676a2170a11b Mon Sep 17 00:00:00 2001 From: rajlaud <50647620+rajlaud@users.noreply.github.com> Date: Fri, 17 Jul 2020 11:21:42 -0500 Subject: [PATCH] Fix bugs updating state of `hdmi_cec` switch (#37786) --- homeassistant/components/hdmi_cec/__init__.py | 11 ++++++++++- homeassistant/components/hdmi_cec/media_player.py | 8 ++++---- homeassistant/components/hdmi_cec/switch.py | 13 ++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/hdmi_cec/__init__.py b/homeassistant/components/hdmi_cec/__init__.py index 471a2dd0f46..c9a5d27a3be 100644 --- a/homeassistant/components/hdmi_cec/__init__.py +++ b/homeassistant/components/hdmi_cec/__init__.py @@ -353,7 +353,7 @@ def setup(hass: HomeAssistant, base_config): return True -class CecDevice(Entity): +class CecEntity(Entity): """Representation of a HDMI CEC device entity.""" def __init__(self, device, logical) -> None: @@ -388,6 +388,15 @@ class CecDevice(Entity): """Device status changed, schedule an update.""" self.schedule_update_ha_state(True) + @property + def should_poll(self): + """ + Return false. + + CecEntity.update() is called by the HDMI network when there is new data. + """ + return False + @property def name(self): """Return the name of the device.""" diff --git a/homeassistant/components/hdmi_cec/media_player.py b/homeassistant/components/hdmi_cec/media_player.py index 180580ef371..c3cab6a8f98 100644 --- a/homeassistant/components/hdmi_cec/media_player.py +++ b/homeassistant/components/hdmi_cec/media_player.py @@ -43,7 +43,7 @@ from homeassistant.const import ( STATE_PLAYING, ) -from . import ATTR_NEW, CecDevice +from . import ATTR_NEW, CecEntity _LOGGER = logging.getLogger(__name__) @@ -57,16 +57,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None): entities = [] for device in discovery_info[ATTR_NEW]: hdmi_device = hass.data.get(device) - entities.append(CecPlayerDevice(hdmi_device, hdmi_device.logical_address)) + entities.append(CecPlayerEntity(hdmi_device, hdmi_device.logical_address)) add_entities(entities, True) -class CecPlayerDevice(CecDevice, MediaPlayerEntity): +class CecPlayerEntity(CecEntity, MediaPlayerEntity): """Representation of a HDMI device as a Media player.""" def __init__(self, device, logical) -> None: """Initialize the HDMI device.""" - CecDevice.__init__(self, device, logical) + CecEntity.__init__(self, device, logical) self.entity_id = f"{DOMAIN}.hdmi_{hex(self._logical_address)[2:]}" def send_keypress(self, key): diff --git a/homeassistant/components/hdmi_cec/switch.py b/homeassistant/components/hdmi_cec/switch.py index aaaa2b83054..ea0cac76a99 100644 --- a/homeassistant/components/hdmi_cec/switch.py +++ b/homeassistant/components/hdmi_cec/switch.py @@ -4,7 +4,7 @@ import logging from homeassistant.components.switch import DOMAIN, SwitchEntity from homeassistant.const import STATE_OFF, STATE_ON, STATE_STANDBY -from . import ATTR_NEW, CecDevice +from . import ATTR_NEW, CecEntity _LOGGER = logging.getLogger(__name__) @@ -18,27 +18,29 @@ def setup_platform(hass, config, add_entities, discovery_info=None): entities = [] for device in discovery_info[ATTR_NEW]: hdmi_device = hass.data.get(device) - entities.append(CecSwitchDevice(hdmi_device, hdmi_device.logical_address)) + entities.append(CecSwitchEntity(hdmi_device, hdmi_device.logical_address)) add_entities(entities, True) -class CecSwitchDevice(CecDevice, SwitchEntity): +class CecSwitchEntity(CecEntity, SwitchEntity): """Representation of a HDMI device as a Switch.""" def __init__(self, device, logical) -> None: """Initialize the HDMI device.""" - CecDevice.__init__(self, device, logical) + CecEntity.__init__(self, device, logical) self.entity_id = f"{DOMAIN}.hdmi_{hex(self._logical_address)[2:]}" def turn_on(self, **kwargs) -> None: """Turn device on.""" self._device.turn_on() self._state = STATE_ON + self.schedule_update_ha_state(force_refresh=False) def turn_off(self, **kwargs) -> None: """Turn device off.""" self._device.turn_off() - self._state = STATE_ON + self._state = STATE_OFF + self.schedule_update_ha_state(force_refresh=False) def toggle(self, **kwargs): """Toggle the entity.""" @@ -47,6 +49,7 @@ class CecSwitchDevice(CecDevice, SwitchEntity): self._state = STATE_OFF else: self._state = STATE_ON + self.schedule_update_ha_state(force_refresh=False) @property def is_on(self) -> bool: