Skip to content

Commit

Permalink
Move hdmi_cec base entity to separate module (#126057)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Sep 16, 2024
1 parent 95db4df commit 4fbc5a9
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 106 deletions.
105 changes: 1 addition & 104 deletions homeassistant/components/hdmi_cec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,15 @@
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"
Expand All @@ -70,12 +55,7 @@
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"
Expand Down Expand Up @@ -156,7 +136,6 @@
)

WATCHDOG_INTERVAL = 120
EVENT_HDMI_CEC_UNAVAILABLE = "hdmi_cec_unavailable"


def pad_physical_address(addr):
Expand Down Expand Up @@ -356,85 +335,3 @@ def _start_cec(callback_event):
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
7 changes: 7 additions & 0 deletions homeassistant/components/hdmi_cec/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Support for HDMI CEC."""

DOMAIN = "hdmi_cec"

ATTR_NEW = "new"

EVENT_HDMI_CEC_UNAVAILABLE = "hdmi_cec_unavailable"
109 changes: 109 additions & 0 deletions homeassistant/components/hdmi_cec/entity.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion homeassistant/components/hdmi_cec/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
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__)

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/hdmi_cec/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
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__)

Expand Down

0 comments on commit 4fbc5a9

Please sign in to comment.