-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_player.py
476 lines (417 loc) · 17.3 KB
/
media_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
"""
Support for Enigma2 set-top boxes.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/enigma/
"""
#
# For more details, please refer to github at
# https://github.com/HengeDK/homeassistant-enigma-player
#
# This is a branch from
# https://github.com/KavajNaruj/homeassistant-enigma-player
#
# Imports and dependencies
import asyncio
from datetime import timedelta
from urllib.error import HTTPError, URLError
import urllib.parse
import urllib.request
import aiohttp
import voluptuous as vol
from datetime import timedelta
from urllib.error import HTTPError, URLError
# From homeassitant
from custom_components.enigma import _LOGGER, DOMAIN as ENIGMA_DOMAIN
from homeassistant.components.media_player.const import (
MEDIA_TYPE_CHANNEL,
MEDIA_TYPE_TVSHOW,
SUPPORT_NEXT_TRACK,
SUPPORT_PLAY,
SUPPORT_PAUSE,
SUPPORT_PLAY_MEDIA,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP
)
from homeassistant.components.media_player import MediaPlayerEntity
from homeassistant.const import (STATE_OFF, STATE_ON, STATE_UNKNOWN)
import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle
# VERSION
VERSION = '1.3'
# Dependencies
DEPENDENCIES = ['enigma']
# DEFAULTS
DEFAULT_PORT = 80
DEFAULT_NAME = "Enigma2 Satelite"
DEFAULT_TIMEOUT = 30
DEFAULT_USERNAME = 'root'
DEFAULT_PASSWORD = ''
DEFAULT_BOUQUET = ''
DEFAULT_PICON = 'picon'
# Return cached results if last scan was less then this time ago.
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=5)
SUPPORT_ENIGMA = (
SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_MUTE
| SUPPORT_TURN_ON
| SUPPORT_TURN_OFF
| SUPPORT_SELECT_SOURCE
| SUPPORT_NEXT_TRACK
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_VOLUME_STEP
| SUPPORT_PLAY
| SUPPORT_PLAY_MEDIA
| SUPPORT_PAUSE
)
MAX_VOLUME = 100
# SETUP PLATFORM
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up platform."""
"""Initialize the Enigma device."""
devices = []
enigma_list = hass.data[ENIGMA_DOMAIN]
for device in enigma_list:
_LOGGER.debug("Configured a new EnigmaMediaPlayer %s",
device.get_host)
devices.append(EnigmaMediaPlayer(device))
async_add_entities(devices, update_before_add=True)
# Enigma Media Player Device
class EnigmaMediaPlayer(MediaPlayerEntity):
"""Representation of a Enigma Media Player device."""
def __init__(self, EnigmaMediaPlayerEntity):
"""Initialize the Enigma device."""
self._host = EnigmaMediaPlayerEntity.get_host
self._port = EnigmaMediaPlayerEntity.get_port
self._name = EnigmaMediaPlayerEntity.get_name
self._username = EnigmaMediaPlayerEntity.get_username
self._password = EnigmaMediaPlayerEntity.get_password
self._timeout = EnigmaMediaPlayerEntity.get_timeout
self._bouquet = EnigmaMediaPlayerEntity.get_bouquet
self._picon = EnigmaMediaPlayerEntity.get_picon
self._opener = EnigmaMediaPlayerEntity.get_opener
self._pwstate = True
self._volume = 0
self._muted = False
self._selected_source = ''
self._selected_media_content_id = ''
self._selected_media_title = ''
self._picon_url = None
self._source_names = {}
self._sources = {}
# Run when added to HASS TO LOAD SOURCES
async def async_added_to_hass(self):
"""Run when entity about to be added."""
await super().async_added_to_hass()
await self.load_sources()
# Load channels from specified bouquet or from first available bouquet
async def load_sources(self):
"""Initialize the Enigma device loading the sources."""
from bs4 import BeautifulSoup
if self._bouquet:
# Load user set bouquet.
_LOGGER.debug("Enigma: [load_sources] - Request user bouquet %s ",
self._bouquet)
epgbouquet_xml = await self.request_call('/web/epgnow?bRef=' +
urllib.parse.quote_plus
(self._bouquet))
# Channels name
soup = BeautifulSoup(epgbouquet_xml, 'html.parser')
src_names = soup.find_all('e2eventservicename')
self._source_names = [src_name.string for src_name in src_names]
# Channels reference
src_references = soup.find_all('e2eventservicereference')
sources = [src_reference.string for src_reference in
src_references]
self._sources = dict(zip(self._source_names, sources))
else:
# Load sources from first bouquet.
reference = urllib.parse.quote_plus(await self.get_bouquet_reference())
_LOGGER.debug("Enigma: [load_sources] - Request reference %s ",
reference)
epgbouquet_xml = await self.request_call('/web/epgnow?bRef=' + reference)
# Channels name
soup = BeautifulSoup(epgbouquet_xml, 'html.parser')
src_names = soup.find_all('e2eventservicename')
self._source_names = [src_name.string for src_name in src_names]
# Channels reference
src_references = soup.find_all('e2eventservicereference')
sources = [src_reference.string for src_reference in
src_references]
self._sources = dict(zip(self._source_names, sources))
async def get_bouquet_reference(self):
"""Import BeautifulSoup."""
from bs4 import BeautifulSoup
# Get first bouquet reference
bouquets_xml = await self.request_call('/web/getallservices')
soup = BeautifulSoup(bouquets_xml, 'html.parser')
return soup.find('e2servicereference').renderContents().decode('UTF8')
# Asnc API requests
async def request_call(self, url):
"""Call web API request."""
uri = 'http://' + self._host + ":" + str(self._port) + url
_LOGGER.debug("Enigma: [request_call] - Call request %s ", uri)
# Check if is password enabled
if self._password is not None:
# Handle HTTP Auth
async with self._opener.get(uri, auth=aiohttp.BasicAuth(self._username, self._password)) as resp:
text = await resp.read()
return text
else:
async with self._opener.get(uri) as resp:
text = await resp.read()
return text
# Component Update
@Throttle(MIN_TIME_BETWEEN_SCANS)
async def async_update(self):
"""Import BeautifulSoup."""
from bs4 import BeautifulSoup
# Get the latest details from the device.
_LOGGER.debug("Enigma: [update] - request for host %s (%s)", self._host,
self._name)
powerstate_xml = await self.request_call('/web/powerstate')
powerstate_soup = BeautifulSoup(powerstate_xml, 'html.parser')
pwstate = powerstate_soup.e2instandby.renderContents().decode('UTF8')
self._pwstate = ''
_LOGGER.debug("Enigma: [update] - Powerstate for host %s = %s",
self._host, pwstate)
if pwstate.find('false') >= 0:
self._pwstate = 'false'
if pwstate.find('true') >= 0:
self._pwstate = 'true'
# If name was not defined, get the name from the box
if self._name == 'Enigma2 Satelite':
about_xml = await self.request_call('/web/about')
soup = BeautifulSoup(about_xml, 'html.parser')
name = soup.e2model.renderContents().decode('UTF8')
_LOGGER.debug("Enigma: [update] - Name for host %s = %s",
self._host, name)
if name:
self._name = name
# If powered on
if self._pwstate == 'false':
subservices_xml = await self.request_call('/web/subservices')
soup = BeautifulSoup(subservices_xml, 'html.parser')
servicename = soup.e2servicename.renderContents().decode('UTF8')
reference = soup.e2servicereference.renderContents().decode('UTF8')
eventid = 'N/A'
eventtitle = 'N/A'
# If we got a valid reference, check the title of the event and
# the picon url
if reference != '' and reference != 'N/A' and \
not reference.startswith('1:0:0:0:0:0:0:0:0:0:'):
xml = await self.request_call('/web/epgservicenow?sRef=' + reference)
soup = BeautifulSoup(xml, 'html.parser')
eventtitle = soup.e2eventtitle.renderContents().decode('UTF8')
eventid = soup.e2eventid.renderContents().decode('UTF8')
if self._password != DEFAULT_PASSWORD:
# if icon = screenhost then get screenshot
if self._picon == 'screenshot':
self._picon_url = 'http://' + \
self._username + ':' + \
self._password + \
'@' + self._host + ':' + \
str(self._port) + '/grab?format=png\
&r=720&mode=all&reference=' + \
reference.replace(":", "_")[:-1]
# otherwise try to get picon
else:
self._picon_url = 'http://' + \
self._username + ':' + \
self._password + \
'@' + self._host + ':' + \
str(self._port) + '/picon/' + \
reference.replace(":", "_")[:-1] \
+ '.png'
else:
# if icon = screenhost then get screenshot
if self._picon == 'screenshot':
self._picon_url = 'http://' + \
self._username + ':' + \
self._password + \
'@' + self._host + ':' + \
str(self._port) + '/grab?format=png\
&r=720&mode=all&reference=' + \
reference.replace(":", "_")[:-1]
# otherwise try to get picon
else:
self._picon_url = 'http://' + self._host + ':' + \
str(self._port) + '/picon/' + \
reference.replace(":", "_")[:-1] \
+ '.png'
_LOGGER.debug("Enigma: [update] - Eventtitle for host %s = %s",
self._host, eventtitle)
# Check volume and if is muted and update self variables
volume_xml = await self.request_call('/web/vol')
soup = BeautifulSoup(volume_xml, 'html.parser')
volcurrent = soup.e2current.renderContents().decode('UTF8')
volmuted = soup.e2ismuted.renderContents().decode('UTF8')
self._volume = int(volcurrent) / MAX_VOLUME if volcurrent else None
self._muted = (volmuted == 'True') if volmuted else None
_LOGGER.debug("Enigma: [update] - Volume for host %s = %s",
self._host, volcurrent)
_LOGGER.debug("Enigma: [update] - Is host %s muted = %s",
self._host, volmuted)
# Info of selected source and title
self._selected_source = servicename
self._selected_media_content_id = eventid
self._selected_media_title = servicename + ' - ' + eventtitle
return True
# GET - Name
@property
def name(self):
"""Return the name of the device."""
return self._name
# GET - State
@property
def state(self):
"""Return the state of the device."""
if self._pwstate == 'true':
return STATE_OFF
if self._pwstate == 'false':
return STATE_ON
return STATE_UNKNOWN
# GET - Volume Level
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return self._volume
# GET - Muted
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._muted
# GET - Features
@property
def supported_features(self):
"""Flag of media commands that are supported."""
return SUPPORT_ENIGMA
# GET - Content type
@property
def media_content_type(self):
"""Content type of current playing media."""
return MEDIA_TYPE_TVSHOW
# GET - Content id - Current Channel name
@property
def media_content_id(self):
"""Service Ref of current playing media."""
return self._selected_media_content_id
# GET - Media title - Current Channel name
@property
def media_title(self):
"""Title of current playing media."""
return self._selected_media_title
# GET - Content picon - Current Channel Picon
# /picon directory must exist in enigma2 box (use symlink if not)
@property
def media_image_url(self):
"""Title of current playing media."""
_LOGGER.debug("Enigma: [media_image_url] - %s", self._picon_url)
return self._picon_url
# GET - Current channel - Current Channel Name
@property
def source(self):
"""Return the current input source."""
return self._selected_source
# GET - Channel list - Channel names from current bouquet
@property
def source_list(self):
"""List of available input sources."""
return self._source_names
# SET - Change channel - From dropbox menu
@asyncio.coroutine
async def async_select_source(self, source):
"""Select input source."""
_LOGGER.debug("Enigma: [async_select_source] - Change source channel")
await self.request_call('/web/zap?sRef=' + self._sources[source])
# SET - Volume up
@asyncio.coroutine
async def async_volume_up(self):
"""Set volume level up."""
await self.request_call('/web/vol?set=up')
# SET - Volume down
@asyncio.coroutine
async def async_volume_down(self):
"""Set volume level down."""
await self.request_call('/web/vol?set=down')
# SET - Volume level
@asyncio.coroutine
async def async_set_volume_level(self, volume):
"""Set volume level, range 0..1."""
volset = str(round(volume * MAX_VOLUME))
await self.request_call('/web/vol?set=set' + volset)
# SET - Volume mute
@asyncio.coroutine
async def async_mute_volume(self, mute):
"""Mute or unmute media player."""
await self.request_call('/web/vol?set=mute')
# SET - Media Play/pause
@asyncio.coroutine
async def async_media_play_pause(self):
"""Simulate play pause media player."""
_LOGGER.debug("Enigma: [play_pause_toogle] - Does nothing")
# SET - Media Play
@asyncio.coroutine
async def async_media_play(self):
"""Send play command."""
_LOGGER.debug("Enigma: [play] - Does nothing")
# SET - Media Pause
@asyncio.coroutine
async def async_media_pause(self):
"""Send media pause command to media player."""
_LOGGER.debug("Enigma: [pause] - Does nothing")
# SET - Change to channel number
@asyncio.coroutine
async def async_play_media(self, media_type, media_id, **kwargs):
"""Support changing a channel."""
if media_type != MEDIA_TYPE_CHANNEL:
_LOGGER.error('Unsupported media type')
return
try:
cv.positive_int(media_id)
except vol.Invalid:
_LOGGER.error('Media ID must be positive integer')
return
# Hack to map remote key press
# 2 Key "1"
# 3 Key "2"
# 4 Key "3"
# 5 Key "4"
# 6 Key "5"
# 7 Key "6"
# 8 Key "7"
# 9 Key "8"
# 10 Key "9"
# 11 Key "0"
for digit in media_id:
if digit == '0':
channel_digit = '11'
else:
channel_digit = int(digit)+1
await self.request_call('/web/remotecontrol?command='+str(channel_digit))
# SET - Turn on
@asyncio.coroutine
async def async_turn_on(self):
"""Turn the media player on."""
await self.request_call('/web/powerstate?newstate=4')
self.async_update()
# SET - Turn of
@asyncio.coroutine
async def async_turn_off(self):
"""Turn off media player."""
await self.request_call('/web/powerstate?newstate=5')
# SET - Next channel
@asyncio.coroutine
async def async_media_next_track(self):
"""Change to next channel."""
await self.request_call('/web/remotecontrol?command=106')
# SET - Previous channel
@asyncio.coroutine
async def async_media_previous_track(self):
"""Change to previous channel."""
await self.request_call('/web/remotecontrol?command=105')