From bb11dc19d3c0ca7798a74b399b139d785d33d725 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 2 Aug 2021 14:30:30 +0200 Subject: [PATCH] Convert @property to _attr_variable for hdmi_sec (#53816) Convert @property to _attr_variable. Break __init__ with a local function. Make _attr_should_poll a class variable. Co-authored-by: Martin Hjelmare --- homeassistant/components/hdmi_cec/__init__.py | 57 +++++++------------ 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/hdmi_cec/__init__.py b/homeassistant/components/hdmi_cec/__init__.py index 471542824de..9d4fa286fd6 100644 --- a/homeassistant/components/hdmi_cec/__init__.py +++ b/homeassistant/components/hdmi_cec/__init__.py @@ -371,13 +371,32 @@ def setup(hass: HomeAssistant, base_config): # noqa: C901 class CecEntity(Entity): """Representation of a HDMI CEC device entity.""" + _attr_should_poll = False + def __init__(self, device, logical) -> None: """Initialize the device.""" self._device = device - self._icon = None self._state: str | None = None self._logical_address = logical self.entity_id = "%s.%d" % (DOMAIN, self._logical_address) + self._set_attr_name() + if self._device.type in ICONS_BY_TYPE: + self._attr_icon = ICONS_BY_TYPE[self._device.type] + else: + self._attr_icon = ICON_UNKNOWN + + def _set_attr_name(self): + """Set name.""" + if ( + self._device.osd_name is not None + and self.vendor_name is not None + and self.vendor_name != "Unknown" + ): + self._attr_name = f"{self.vendor_name} {self._device.osd_name}" + elif self._device.osd_name is None: + self._attr_name = f"{self._device.type_name} {self._logical_address}" + else: + self._attr_name = f"{self._device.type_name} {self._logical_address} ({self._device.osd_name})" def _hdmi_cec_unavailable(self, callback_event): # Change state to unavailable. Without this, entity would remain in @@ -412,31 +431,6 @@ class CecEntity(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.""" - return ( - f"{self.vendor_name} {self._device.osd_name}" - if ( - self._device.osd_name is not None - and self.vendor_name is not None - and self.vendor_name != "Unknown" - ) - else "%s %d" % (self._device.type_name, self._logical_address) - if self._device.osd_name is None - else "%s %d (%s)" - % (self._device.type_name, self._logical_address, self._device.osd_name) - ) - @property def vendor_id(self): """Return the ID of the device's vendor.""" @@ -462,17 +456,6 @@ class CecEntity(Entity): """Return the type ID of device.""" return self._device.type - @property - def icon(self): - """Return the icon for device by its type.""" - return ( - self._icon - if self._icon is not None - else ICONS_BY_TYPE.get(self._device.type) - if self._device.type in ICONS_BY_TYPE - else ICON_UNKNOWN - ) - @property def extra_state_attributes(self): """Return the state attributes."""