2021-06-17 10:33:44 +00:00
|
|
|
"""Remote control support for Bravia TV."""
|
2021-07-03 13:37:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Iterable
|
|
|
|
from typing import Any
|
2021-06-17 10:33:44 +00:00
|
|
|
|
|
|
|
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
|
2021-07-03 13:37:54 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-06-17 10:33:44 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
from . import BraviaTVCoordinator
|
2021-06-21 04:39:14 +00:00
|
|
|
from .const import ATTR_MANUFACTURER, DEFAULT_NAME, DOMAIN
|
2021-06-17 10:33:44 +00:00
|
|
|
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-06-17 10:33:44 +00:00
|
|
|
"""Set up Bravia TV Remote from a config entry."""
|
|
|
|
|
2021-06-21 04:39:14 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2021-06-17 10:33:44 +00:00
|
|
|
unique_id = config_entry.unique_id
|
2021-07-03 13:37:54 +00:00
|
|
|
assert unique_id is not None
|
2021-10-23 09:41:32 +00:00
|
|
|
device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, unique_id)},
|
|
|
|
manufacturer=ATTR_MANUFACTURER,
|
|
|
|
model=config_entry.title,
|
|
|
|
name=DEFAULT_NAME,
|
|
|
|
)
|
2021-06-17 10:33:44 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[BraviaTVRemote(coordinator, DEFAULT_NAME, unique_id, device_info)]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BraviaTVRemote(CoordinatorEntity, RemoteEntity):
|
|
|
|
"""Representation of a Bravia TV Remote."""
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
coordinator: BraviaTVCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: BraviaTVCoordinator,
|
|
|
|
name: str,
|
|
|
|
unique_id: str,
|
|
|
|
device_info: DeviceInfo,
|
|
|
|
) -> None:
|
2021-06-17 10:33:44 +00:00
|
|
|
"""Initialize the entity."""
|
|
|
|
|
2021-06-23 13:46:28 +00:00
|
|
|
self._attr_device_info = device_info
|
|
|
|
self._attr_name = name
|
|
|
|
self._attr_unique_id = unique_id
|
2021-06-17 10:33:44 +00:00
|
|
|
|
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
@property
|
2021-07-03 13:37:54 +00:00
|
|
|
def is_on(self) -> bool:
|
2021-06-17 10:33:44 +00:00
|
|
|
"""Return true if device is on."""
|
|
|
|
return self.coordinator.is_on
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2021-06-17 10:33:44 +00:00
|
|
|
"""Turn the device on."""
|
|
|
|
await self.coordinator.async_turn_on()
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2021-06-17 10:33:44 +00:00
|
|
|
"""Turn the device off."""
|
|
|
|
await self.coordinator.async_turn_off()
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
|
2021-06-17 10:33:44 +00:00
|
|
|
"""Send a command to device."""
|
|
|
|
repeats = kwargs[ATTR_NUM_REPEATS]
|
|
|
|
await self.coordinator.async_send_command(command, repeats)
|