2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HDMI CEC devices as switches."""
|
2021-07-31 21:18:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-01-20 20:39:18 +00:00
|
|
|
import logging
|
|
|
|
|
2022-02-14 11:43:36 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchEntity
|
2021-09-03 16:17:41 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2022-01-10 09:35:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-01-20 20:39:18 +00:00
|
|
|
|
2022-02-14 11:43:36 +00:00
|
|
|
from . import ATTR_NEW, DOMAIN, CecEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-01-20 20:39:18 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-02-14 11:43:36 +00:00
|
|
|
ENTITY_ID_FORMAT = SWITCH_DOMAIN + ".{}"
|
2017-01-20 20:39:18 +00:00
|
|
|
|
|
|
|
|
2022-01-10 09:35:19 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-01-20 20:39:18 +00:00
|
|
|
"""Find and return HDMI devices as switches."""
|
2022-01-10 09:35:19 +00:00
|
|
|
if discovery_info and ATTR_NEW in discovery_info:
|
2017-01-20 20:39:18 +00:00
|
|
|
_LOGGER.info("Setting up HDMI devices %s", discovery_info[ATTR_NEW])
|
2018-12-02 14:51:04 +00:00
|
|
|
entities = []
|
|
|
|
for device in discovery_info[ATTR_NEW]:
|
2022-01-10 09:57:48 +00:00
|
|
|
hdmi_device = hass.data[DOMAIN][device]
|
2020-07-17 16:21:42 +00:00
|
|
|
entities.append(CecSwitchEntity(hdmi_device, hdmi_device.logical_address))
|
2018-12-02 14:51:04 +00:00
|
|
|
add_entities(entities, True)
|
2017-01-20 20:39:18 +00:00
|
|
|
|
|
|
|
|
2020-07-17 16:21:42 +00:00
|
|
|
class CecSwitchEntity(CecEntity, SwitchEntity):
|
2017-01-20 20:39:18 +00:00
|
|
|
"""Representation of a HDMI device as a Switch."""
|
|
|
|
|
2018-12-02 14:51:04 +00:00
|
|
|
def __init__(self, device, logical) -> None:
|
2017-01-20 20:39:18 +00:00
|
|
|
"""Initialize the HDMI device."""
|
2020-07-17 16:21:42 +00:00
|
|
|
CecEntity.__init__(self, device, logical)
|
2022-02-14 11:43:36 +00:00
|
|
|
self.entity_id = f"{SWITCH_DOMAIN}.hdmi_{hex(self._logical_address)[2:]}"
|
2017-01-20 20:39:18 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs) -> None:
|
|
|
|
"""Turn device on."""
|
|
|
|
self._device.turn_on()
|
2021-09-03 16:17:41 +00:00
|
|
|
self._state = STATE_ON
|
2020-07-17 16:21:42 +00:00
|
|
|
self.schedule_update_ha_state(force_refresh=False)
|
2017-01-20 20:39:18 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Turn device off."""
|
|
|
|
self._device.turn_off()
|
2021-09-03 16:17:41 +00:00
|
|
|
self._state = STATE_OFF
|
2020-07-17 16:21:42 +00:00
|
|
|
self.schedule_update_ha_state(force_refresh=False)
|
2017-01-20 20:39:18 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def toggle(self, **kwargs):
|
2017-01-23 21:22:39 +00:00
|
|
|
"""Toggle the entity."""
|
|
|
|
self._device.toggle()
|
2021-09-03 16:17:41 +00:00
|
|
|
if self._state == STATE_ON:
|
|
|
|
self._state = STATE_OFF
|
|
|
|
else:
|
|
|
|
self._state = STATE_ON
|
2020-07-17 16:21:42 +00:00
|
|
|
self.schedule_update_ha_state(force_refresh=False)
|
2021-09-03 16:17:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self._state == STATE_ON
|