Home Assistant Unofficial Reference 2024.12.1
remote.py
Go to the documentation of this file.
1 """Remote control support for Panasonic Viera TV."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 from typing import Any
7 
8 from homeassistant.components.remote import RemoteEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_NAME, STATE_ON
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import Remote
16 from .const import (
17  ATTR_DEVICE_INFO,
18  ATTR_MANUFACTURER,
19  ATTR_MODEL_NUMBER,
20  ATTR_REMOTE,
21  ATTR_UDN,
22  DEFAULT_MANUFACTURER,
23  DEFAULT_MODEL_NUMBER,
24  DOMAIN,
25 )
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up Panasonic Viera TV Remote from a config entry."""
34 
35  config = config_entry.data
36 
37  remote = hass.data[DOMAIN][config_entry.entry_id][ATTR_REMOTE]
38  name = config[CONF_NAME]
39  device_info = config[ATTR_DEVICE_INFO]
40 
41  async_add_entities([PanasonicVieraRemoteEntity(remote, name, device_info)])
42 
43 
45  """Representation of a Panasonic Viera TV Remote."""
46 
47  def __init__(
48  self, remote: Remote, name: str, device_info: dict[str, Any] | None = None
49  ) -> None:
50  """Initialize the entity."""
51  # Save a reference to the imported class
52  self._remote_remote = remote
53  self._name_name = name
54  self._device_info_device_info = device_info
55 
56  @property
57  def unique_id(self) -> str | None:
58  """Return the unique ID of the device."""
59  if self._device_info_device_info is None:
60  return None
61  return self._device_info_device_info[ATTR_UDN]
62 
63  @property
64  def device_info(self) -> DeviceInfo | None:
65  """Return device specific attributes."""
66  if self._device_info_device_info is None:
67  return None
68  return DeviceInfo(
69  identifiers={(DOMAIN, self._device_info_device_info[ATTR_UDN])},
70  manufacturer=self._device_info_device_info.get(ATTR_MANUFACTURER, DEFAULT_MANUFACTURER),
71  model=self._device_info_device_info.get(ATTR_MODEL_NUMBER, DEFAULT_MODEL_NUMBER),
72  name=self._name_name,
73  )
74 
75  @property
76  def name(self) -> str:
77  """Return the name of the device."""
78  return self._name_name
79 
80  @property
81  def available(self) -> bool:
82  """Return True if the device is available."""
83  return self._remote_remote.available
84 
85  @property
86  def is_on(self) -> bool:
87  """Return true if device is on."""
88  return self._remote_remote.state == STATE_ON
89 
90  async def async_turn_on(self, **kwargs: Any) -> None:
91  """Turn the device on."""
92  await self._remote_remote.async_turn_on(context=self._context_context)
93 
94  async def async_turn_off(self, **kwargs: Any) -> None:
95  """Turn the device off."""
96  await self._remote_remote.async_turn_off()
97 
98  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
99  """Send a command to one device."""
100  for cmd in command:
101  await self._remote_remote.async_send_key(cmd)
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:98
None __init__(self, Remote remote, str name, dict[str, Any]|None device_info=None)
Definition: remote.py:49
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:32