1 """Support for the Unitymedia Horizon HD Recorder."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from horimote
import Client, keys
10 from horimote.exceptions
import AuthenticationError
11 import voluptuous
as vol
13 from homeassistant
import util
15 PLATFORM_SCHEMA
as MEDIA_PLAYER_PLATFORM_SCHEMA,
17 MediaPlayerEntityFeature,
28 _LOGGER = logging.getLogger(__name__)
30 DEFAULT_NAME =
"Horizon"
33 MIN_TIME_BETWEEN_FORCED_SCANS =
timedelta(seconds=1)
37 PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
39 vol.Required(CONF_HOST): cv.string,
40 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
41 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
49 add_entities: AddEntitiesCallback,
50 discovery_info: DiscoveryInfoType |
None =
None,
52 """Set up the Horizon platform."""
54 host = config[CONF_HOST]
55 name = config[CONF_NAME]
56 port = config[CONF_PORT]
59 client = Client(host, port=port)
60 except AuthenticationError
as msg:
61 _LOGGER.error(
"Authentication to %s at %s failed: %s", name, host, msg)
63 except OSError
as msg:
65 _LOGGER.error(
"Connection to %s at %s failed: %s", name, host, msg)
66 raise PlatformNotReady
from msg
68 _LOGGER.debug(
"Connection to %s at %s established", name, host)
74 """Representation of a Horizon HD Recorder."""
76 _attr_supported_features = (
77 MediaPlayerEntityFeature.NEXT_TRACK
78 | MediaPlayerEntityFeature.PAUSE
79 | MediaPlayerEntityFeature.PLAY
80 | MediaPlayerEntityFeature.PLAY_MEDIA
81 | MediaPlayerEntityFeature.PREVIOUS_TRACK
82 | MediaPlayerEntityFeature.TURN_ON
83 | MediaPlayerEntityFeature.TURN_OFF
87 """Initialize the remote."""
94 """Return the name of the remote."""
95 return self.
_name_name
97 @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
99 """Update State using the media server running on the Horizon."""
101 if self.
_client_client.is_powered_on():
109 """Turn the device on."""
114 """Turn the device off."""
121 self.
_attr_state_attr_state = MediaPlayerState.PLAYING
126 self.
_attr_state_attr_state = MediaPlayerState.PLAYING
129 """Send play command."""
131 self.
_attr_state_attr_state = MediaPlayerState.PLAYING
134 """Send pause command."""
136 self.
_attr_state_attr_state = MediaPlayerState.PAUSED
139 """Send play/pause command."""
142 self.
_attr_state_attr_state = MediaPlayerState.PLAYING
144 self.
_attr_state_attr_state = MediaPlayerState.PAUSED
147 self, media_type: MediaType | str, media_id: str, **kwargs: Any
149 """Play media / switch to channel."""
150 if media_type == MediaType.CHANNEL:
153 self.
_attr_state_attr_state = MediaPlayerState.PLAYING
155 _LOGGER.error(
"Invalid channel: %s", media_id)
158 "Invalid media type %s. Supported type: %s",
164 """Select a channel (taken from einder library, thx)."""
165 self.
_send_send(channel=channel)
168 """Send a key to the Horizon device."""
169 self.
_send_send(key=key)
171 def _send(self, key=None, channel=None):
172 """Send a key to the Horizon device."""
176 self.
_client_client.send_key(key)
178 self.
_client_client.select_channel(channel)
179 except OSError
as msg:
180 _LOGGER.error(
"%s disconnected: %s. Trying to reconnect", self.
_name_name, msg)
183 self.
_client_client.disconnect()
187 self.
_client_client.authorize()
188 except AuthenticationError
as msg2:
189 _LOGGER.error(
"Authentication to %s failed: %s", self.
_name_name, msg2)
191 except OSError
as msg2:
193 _LOGGER.error(
"Reconnect to %s failed: %s", self.
_name_name, msg2)
196 self.
_send_send(key=key, channel=channel)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)