diff --git a/homeassistant/components/hdmi_cec/__init__.py b/homeassistant/components/hdmi_cec/__init__.py index 9d208b3a228..6b4a949c0fc 100644 --- a/homeassistant/components/hdmi_cec/__init__.py +++ b/homeassistant/components/hdmi_cec/__init__.py @@ -35,30 +35,15 @@ from homeassistant.const import ( from homeassistant.core import HassJob, HomeAssistant, ServiceCall, callback from homeassistant.helpers import discovery, event import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ConfigType -DOMAIN = "hdmi_cec" +from .const import DOMAIN, EVENT_HDMI_CEC_UNAVAILABLE _LOGGER = logging.getLogger(__name__) DEFAULT_DISPLAY_NAME = "HA" CONF_TYPES = "types" -ICON_UNKNOWN = "mdi:help" -ICON_AUDIO = "mdi:speaker" -ICON_PLAYER = "mdi:play" -ICON_TUNER = "mdi:radio" -ICON_RECORDER = "mdi:microphone" -ICON_TV = "mdi:television" -ICONS_BY_TYPE = { - 0: ICON_TV, - 1: ICON_RECORDER, - 3: ICON_TUNER, - 4: ICON_PLAYER, - 5: ICON_AUDIO, -} - CMD_UP = "up" CMD_DOWN = "down" CMD_MUTE = "mute" @@ -70,12 +55,7 @@ CMD_RELEASE = "release" EVENT_CEC_COMMAND_RECEIVED = "cec_command_received" EVENT_CEC_KEYPRESS_RECEIVED = "cec_keypress_received" -ATTR_PHYSICAL_ADDRESS = "physical_address" -ATTR_TYPE_ID = "type_id" -ATTR_VENDOR_NAME = "vendor_name" -ATTR_VENDOR_ID = "vendor_id" ATTR_DEVICE = "device" -ATTR_TYPE = "type" ATTR_KEY = "key" ATTR_DUR = "dur" ATTR_SRC = "src" @@ -156,7 +136,6 @@ CONFIG_SCHEMA = vol.Schema( ) WATCHDOG_INTERVAL = 120 -EVENT_HDMI_CEC_UNAVAILABLE = "hdmi_cec_unavailable" def pad_physical_address(addr): @@ -356,85 +335,3 @@ def setup(hass: HomeAssistant, base_config: ConfigType) -> bool: # noqa: C901 hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start_cec) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) return True - - -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._logical_address = logical - self.entity_id = "%s.%d" % (DOMAIN, self._logical_address) - self._set_attr_name() - self._attr_icon = ICONS_BY_TYPE.get(self._device.type, 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): - self._attr_available = False - self.schedule_update_ha_state(False) - - async def async_added_to_hass(self): - """Register HDMI callbacks after initialization.""" - self._device.set_update_callback(self._update) - self.hass.bus.async_listen( - EVENT_HDMI_CEC_UNAVAILABLE, self._hdmi_cec_unavailable - ) - - def _update(self, device=None): - """Device status changed, schedule an update.""" - self._attr_available = True - self.schedule_update_ha_state(True) - - @property - def vendor_id(self): - """Return the ID of the device's vendor.""" - return self._device.vendor_id - - @property - def vendor_name(self): - """Return the name of the device's vendor.""" - return self._device.vendor - - @property - def physical_address(self): - """Return the physical address of device in HDMI network.""" - return str(self._device.physical_address) - - @property - def type(self): - """Return a string representation of the device's type.""" - return self._device.type_name - - @property - def type_id(self): - """Return the type ID of device.""" - return self._device.type - - @property - def extra_state_attributes(self): - """Return the state attributes.""" - state_attr = {} - if self.vendor_id is not None: - state_attr[ATTR_VENDOR_ID] = self.vendor_id - state_attr[ATTR_VENDOR_NAME] = self.vendor_name - if self.type_id is not None: - state_attr[ATTR_TYPE_ID] = self.type_id - state_attr[ATTR_TYPE] = self.type - if self.physical_address is not None: - state_attr[ATTR_PHYSICAL_ADDRESS] = self.physical_address - return state_attr diff --git a/homeassistant/components/hdmi_cec/const.py b/homeassistant/components/hdmi_cec/const.py new file mode 100644 index 00000000000..beb95e95676 --- /dev/null +++ b/homeassistant/components/hdmi_cec/const.py @@ -0,0 +1,7 @@ +"""Support for HDMI CEC.""" + +DOMAIN = "hdmi_cec" + +ATTR_NEW = "new" + +EVENT_HDMI_CEC_UNAVAILABLE = "hdmi_cec_unavailable" diff --git a/homeassistant/components/hdmi_cec/entity.py b/homeassistant/components/hdmi_cec/entity.py new file mode 100644 index 00000000000..b1bcb2720d4 --- /dev/null +++ b/homeassistant/components/hdmi_cec/entity.py @@ -0,0 +1,109 @@ +"""Support for HDMI CEC.""" + +from __future__ import annotations + +from homeassistant.helpers.entity import Entity + +from .const import DOMAIN, EVENT_HDMI_CEC_UNAVAILABLE + +ATTR_PHYSICAL_ADDRESS = "physical_address" +ATTR_TYPE = "type" +ATTR_TYPE_ID = "type_id" +ATTR_VENDOR_NAME = "vendor_name" +ATTR_VENDOR_ID = "vendor_id" + +ICON_UNKNOWN = "mdi:help" +ICON_AUDIO = "mdi:speaker" +ICON_PLAYER = "mdi:play" +ICON_TUNER = "mdi:radio" +ICON_RECORDER = "mdi:microphone" +ICON_TV = "mdi:television" +ICONS_BY_TYPE = { + 0: ICON_TV, + 1: ICON_RECORDER, + 3: ICON_TUNER, + 4: ICON_PLAYER, + 5: ICON_AUDIO, +} + + +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._logical_address = logical + self.entity_id = "%s.%d" % (DOMAIN, self._logical_address) + self._set_attr_name() + self._attr_icon = ICONS_BY_TYPE.get(self._device.type, 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): + self._attr_available = False + self.schedule_update_ha_state(False) + + async def async_added_to_hass(self): + """Register HDMI callbacks after initialization.""" + self._device.set_update_callback(self._update) + self.hass.bus.async_listen( + EVENT_HDMI_CEC_UNAVAILABLE, self._hdmi_cec_unavailable + ) + + def _update(self, device=None): + """Device status changed, schedule an update.""" + self._attr_available = True + self.schedule_update_ha_state(True) + + @property + def vendor_id(self): + """Return the ID of the device's vendor.""" + return self._device.vendor_id + + @property + def vendor_name(self): + """Return the name of the device's vendor.""" + return self._device.vendor + + @property + def physical_address(self): + """Return the physical address of device in HDMI network.""" + return str(self._device.physical_address) + + @property + def type(self): + """Return a string representation of the device's type.""" + return self._device.type_name + + @property + def type_id(self): + """Return the type ID of device.""" + return self._device.type + + @property + def extra_state_attributes(self): + """Return the state attributes.""" + state_attr = {} + if self.vendor_id is not None: + state_attr[ATTR_VENDOR_ID] = self.vendor_id + state_attr[ATTR_VENDOR_NAME] = self.vendor_name + if self.type_id is not None: + state_attr[ATTR_TYPE_ID] = self.type_id + state_attr[ATTR_TYPE] = self.type + if self.physical_address is not None: + state_attr[ATTR_PHYSICAL_ADDRESS] = self.physical_address + return state_attr diff --git a/homeassistant/components/hdmi_cec/media_player.py b/homeassistant/components/hdmi_cec/media_player.py index e86a1f5be70..7ad06f0c45a 100644 --- a/homeassistant/components/hdmi_cec/media_player.py +++ b/homeassistant/components/hdmi_cec/media_player.py @@ -37,7 +37,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import ATTR_NEW, DOMAIN, CecEntity +from .const import ATTR_NEW, DOMAIN +from .entity import CecEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/hdmi_cec/switch.py b/homeassistant/components/hdmi_cec/switch.py index 95998f44a9a..d1bb603a938 100644 --- a/homeassistant/components/hdmi_cec/switch.py +++ b/homeassistant/components/hdmi_cec/switch.py @@ -12,7 +12,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import ATTR_NEW, DOMAIN, CecEntity +from .const import ATTR_NEW, DOMAIN +from .entity import CecEntity _LOGGER = logging.getLogger(__name__)