Home Assistant Unofficial Reference 2024.12.1
media_player.py
Go to the documentation of this file.
1 """Add support for the Xiaomi TVs."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import pymitv
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA,
12  MediaPlayerEntity,
13  MediaPlayerEntityFeature,
14  MediaPlayerState,
15 )
16 from homeassistant.const import CONF_HOST, CONF_NAME
17 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 
22 DEFAULT_NAME = "Xiaomi TV"
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 # No host is needed for configuration, however it can be set.
27 PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
28  {
29  vol.Optional(CONF_HOST): cv.string,
30  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
31  }
32 )
33 
34 
36  hass: HomeAssistant,
37  config: ConfigType,
38  add_entities: AddEntitiesCallback,
39  discovery_info: DiscoveryInfoType | None = None,
40 ) -> None:
41  """Set up the Xiaomi TV platform."""
42 
43  # If a hostname is set. Discovery is skipped.
44  host = config.get(CONF_HOST)
45  name = config.get(CONF_NAME)
46 
47  if host is not None:
48  # Check if there's a valid TV at the IP address.
49  if not pymitv.Discover().check_ip(host):
50  _LOGGER.error("Could not find Xiaomi TV with specified IP: %s", host)
51  else:
52  # Register TV with Home Assistant.
53  add_entities([XiaomiTV(host, name)])
54  else:
55  # Otherwise, discover TVs on network.
56  add_entities(XiaomiTV(tv, DEFAULT_NAME) for tv in pymitv.Discover().scan())
57 
58 
60  """Represent the Xiaomi TV for Home Assistant."""
61 
62  _attr_assumed_state = True
63  _attr_supported_features = (
64  MediaPlayerEntityFeature.VOLUME_STEP
65  | MediaPlayerEntityFeature.TURN_ON
66  | MediaPlayerEntityFeature.TURN_OFF
67  )
68 
69  def __init__(self, ip, name):
70  """Receive IP address and name to construct class."""
71 
72  # Initialize the Xiaomi TV.
73  self._tv_tv = pymitv.TV(ip)
74  # Default name value, only to be overridden by user.
75  self._attr_name_attr_name = name
76  self._attr_state_attr_state = MediaPlayerState.OFF
77 
78  def turn_off(self) -> None:
79  """Instruct the TV to turn sleep.
80 
81  This is done instead of turning off,
82  because the TV won't accept any input when turned off. Thus, the user
83  would be unable to turn the TV back on, unless it's done manually.
84  """
85  if self.statestatestatestate != MediaPlayerState.OFF:
86  self._tv_tv.sleep()
87 
88  self._attr_state_attr_state = MediaPlayerState.OFF
89 
90  def turn_on(self) -> None:
91  """Wake the TV back up from sleep."""
92  if self.statestatestatestate != MediaPlayerState.ON:
93  self._tv_tv.wake()
94 
95  self._attr_state_attr_state = MediaPlayerState.ON
96 
97  def volume_up(self) -> None:
98  """Increase volume by one."""
99  self._tv_tv.volume_up()
100 
101  def volume_down(self) -> None:
102  """Decrease volume by one."""
103  self._tv_tv.volume_down()
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: media_player.py:40