Home Assistant Unofficial Reference 2024.12.1
media_player.py
Go to the documentation of this file.
1 """Support for Panasonic Blu-ray players."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 
7 from panacotta import PanasonicBD
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 from homeassistant.util.dt import utcnow
22 
23 DEFAULT_NAME = "Panasonic Blu-Ray"
24 
25 SCAN_INTERVAL = timedelta(seconds=30)
26 
27 
28 PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
29  {
30  vol.Required(CONF_HOST): cv.string,
31  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
32  }
33 )
34 
35 
37  hass: HomeAssistant,
38  config: ConfigType,
39  add_entities: AddEntitiesCallback,
40  discovery_info: DiscoveryInfoType | None = None,
41 ) -> None:
42  """Set up the Panasonic Blu-ray platform."""
43  conf = discovery_info if discovery_info else config
44 
45  # Register configured device with Home Assistant.
46  add_entities([PanasonicBluRay(conf[CONF_HOST], conf[CONF_NAME])])
47 
48 
50  """Representation of a Panasonic Blu-ray device."""
51 
52  _attr_icon = "mdi:disc-player"
53  _attr_supported_features = (
54  MediaPlayerEntityFeature.TURN_ON
55  | MediaPlayerEntityFeature.TURN_OFF
56  | MediaPlayerEntityFeature.PLAY
57  | MediaPlayerEntityFeature.STOP
58  | MediaPlayerEntityFeature.PAUSE
59  )
60 
61  def __init__(self, ip, name):
62  """Initialize the Panasonic Blue-ray device."""
63  self._device_device = PanasonicBD(ip)
64  self._attr_name_attr_name = name
65  self._attr_state_attr_state = MediaPlayerState.OFF
66  self._attr_media_position_attr_media_position = 0
67  self._attr_media_duration_attr_media_duration = 0
68 
69  def update(self) -> None:
70  """Update the internal state by querying the device."""
71  # This can take 5+ seconds to complete
72  state = self._device_device.get_play_status()
73 
74  if state[0] == "error":
75  self._attr_state_attr_state = None
76  elif state[0] in ["off", "standby"]:
77  # We map both of these to off. If it's really off we can't
78  # turn it on, but from standby we can go to idle by pressing
79  # POWER.
80  self._attr_state_attr_state = MediaPlayerState.OFF
81  elif state[0] in ["paused", "stopped"]:
82  self._attr_state_attr_state = MediaPlayerState.IDLE
83  elif state[0] == "playing":
84  self._attr_state_attr_state = MediaPlayerState.PLAYING
85 
86  # Update our current media position + length
87  if state[1] >= 0:
88  self._attr_media_position_attr_media_position = state[1]
89  else:
90  self._attr_media_position_attr_media_position = 0
91  self._attr_media_position_updated_at_attr_media_position_updated_at = utcnow()
92  self._attr_media_duration_attr_media_duration = state[2]
93 
94  def turn_off(self) -> None:
95  """Instruct the device to turn standby.
96 
97  Sending the "POWER" button will turn the device to standby - there
98  is no way to turn it completely off remotely. However this works in
99  our favour as it means the device is still accepting commands and we
100  can thus turn it back on when desired.
101  """
102  if self.statestatestatestatestate != MediaPlayerState.OFF:
103  self._device_device.send_key("POWER")
104 
105  self._attr_state_attr_state = MediaPlayerState.OFF
106 
107  def turn_on(self) -> None:
108  """Wake the device back up from standby."""
109  if self.statestatestatestatestate == MediaPlayerState.OFF:
110  self._device_device.send_key("POWER")
111 
112  self._attr_state_attr_state = MediaPlayerState.IDLE
113 
114  def media_play(self) -> None:
115  """Send play command."""
116  self._device_device.send_key("PLAYBACK")
117 
118  def media_pause(self) -> None:
119  """Send pause command."""
120  self._device_device.send_key("PAUSE")
121 
122  def media_stop(self) -> None:
123  """Send stop command."""
124  self._device_device.send_key("STOP")
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:41