2022-01-04 15:14:44 +00:00
|
|
|
"""Philips TV menu switches."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2022-01-19 06:44:03 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-04 15:14:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
from . import PhilipsTVDataUpdateCoordinator
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
2022-01-19 06:44:03 +00:00
|
|
|
config_entry: ConfigEntry,
|
2022-01-04 15:14:44 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-19 06:44:03 +00:00
|
|
|
) -> None:
|
2022-01-04 15:14:44 +00:00
|
|
|
"""Set up the configuration entry."""
|
|
|
|
coordinator: PhilipsTVDataUpdateCoordinator = hass.data[DOMAIN][
|
|
|
|
config_entry.entry_id
|
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities([PhilipsTVScreenSwitch(coordinator)])
|
|
|
|
|
|
|
|
|
|
|
|
class PhilipsTVScreenSwitch(CoordinatorEntity, SwitchEntity):
|
|
|
|
"""A Philips TV screen state switch."""
|
|
|
|
|
|
|
|
coordinator: PhilipsTVDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: PhilipsTVDataUpdateCoordinator,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize entity."""
|
|
|
|
|
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
self._attr_name = f"{coordinator.system['name']} Screen State"
|
|
|
|
self._attr_icon = "mdi:television-shimmer"
|
|
|
|
self._attr_unique_id = f"{coordinator.unique_id}_screenstate"
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={
|
|
|
|
(DOMAIN, coordinator.unique_id),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return true if entity is available."""
|
|
|
|
if not super().available:
|
|
|
|
return False
|
|
|
|
if not self.coordinator.api.on:
|
|
|
|
return False
|
|
|
|
return self.coordinator.api.powerstate == "On"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self.coordinator.api.screenstate == "On"
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the entity on."""
|
|
|
|
await self.coordinator.api.setScreenState("On")
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the entity off."""
|
|
|
|
await self.coordinator.api.setScreenState("Off")
|