6
6
from homeassistant .components .media_player import (
7
7
MediaPlayerDeviceClass ,
8
8
MediaPlayerEntity ,
9
+ MediaPlayerEntityFeature ,
9
10
SUPPORT_PAUSE ,
10
11
SUPPORT_PLAY ,
11
12
SUPPORT_STOP ,
@@ -77,6 +78,8 @@ async def async_setup_entry(
77
78
for output_idx , output_info in enumerate (coordinator .outputs )
78
79
]
79
80
81
+ entities .append (JtechMasterMediaPlayer (config_entry , coordinator ))
82
+
80
83
# Add the media player entities to Home Assistant
81
84
async_add_entities (entities , update_before_add = True )
82
85
@@ -194,7 +197,7 @@ def device_info(self) -> DeviceInfo:
194
197
195
198
return DeviceInfo (
196
199
identifiers = { (DOMAIN , self .unique_id ) },
197
- default_name = output_info .name if output_info else f"Output { self ._output_index } " ,
200
+ name = output_info .name if output_info else f"Output { self ._output_index } " ,
198
201
manufacturer = ATTR_MANUFACTURER ,
199
202
model = self ._coordinator .data ["model" ],
200
203
sw_version = self ._coordinator .data ["version" ],
@@ -246,6 +249,7 @@ async def async_select_source(self, source):
246
249
if source_info .name == source :
247
250
await self ._coordinator .async_select_source (self ._output_index , source_idx + 1 )
248
251
break
252
+ await self ._coordinator .async_request_refresh ()
249
253
250
254
async def async_turn_on (self ):
251
255
"""Enable the output and send necessary CEC commands to turn on the connected devices."""
@@ -272,6 +276,7 @@ async def async_turn_on(self):
272
276
if delay_source and delay_source > 0 :
273
277
await asyncio .sleep (delay_source )
274
278
await self ._coordinator .async_send_cec_output (self ._output_index , 5 )
279
+ await self ._coordinator .async_request_refresh ()
275
280
276
281
async def async_turn_off (self ):
277
282
"""Disable the output."""
@@ -292,6 +297,7 @@ async def async_turn_off(self):
292
297
await self ._coordinator .async_disable_output (self ._output_index )
293
298
if cat_stream_toggle :
294
299
await self ._coordinator .async_disable_cat_output (self ._output_index )
300
+ await self ._coordinator .async_request_refresh ()
295
301
296
302
297
303
async def async_volume_up (self ):
@@ -373,3 +379,71 @@ async def _handle_coordinator_update(self) -> None:
373
379
# Trigger an entity state update to reflect the latest data from the coordinator
374
380
_LOGGER .debug ("handle_coordinator_update_data" , self ._coordinator .data )
375
381
self .async_write_ha_state ()
382
+
383
+
384
+ class JtechMasterMediaPlayer (MediaPlayerEntity ):
385
+
386
+ def __init__ (self , config_entry : ConfigEntry , coordinator : JtechCoordinator ):
387
+ """Initialize the media player."""
388
+ self ._config_entry = config_entry
389
+ self ._coordinator = coordinator
390
+
391
+ @property
392
+ def unique_id (self ):
393
+ """Return a unique ID for the media player."""
394
+ return f"{ self ._config_entry .unique_id } _master"
395
+
396
+ @property
397
+ def supported_features (self ):
398
+ """Flag media player features that are supported."""
399
+ return (
400
+ MediaPlayerEntityFeature .TURN_ON
401
+ | MediaPlayerEntityFeature .TURN_OFF
402
+ )
403
+
404
+ @property
405
+ def device_info (self ) -> DeviceInfo :
406
+ """Return the device info."""
407
+
408
+ return DeviceInfo (
409
+ identifiers = { (DOMAIN , self .unique_id ) },
410
+ name = f"{ ATTR_MANUFACTURER } HDMI Matrix" ,
411
+ manufacturer = ATTR_MANUFACTURER ,
412
+ model = self ._coordinator .data ["model" ],
413
+ sw_version = self ._coordinator .data ["version" ],
414
+ via_device = (DOMAIN , self ._config_entry .unique_id ),
415
+ configuration_url = f"http://{ self ._coordinator .data ['hostname' ]} " if self ._coordinator .data ["hostname" ] else None
416
+ )
417
+
418
+ @property
419
+ def device_class (self ):
420
+ """Return the device class."""
421
+ return MediaPlayerDeviceClass .TV
422
+
423
+ @property
424
+ def state (self ):
425
+ """Return the state of the media player."""
426
+ power = self ._coordinator .data .get ("power" , None )
427
+ if power is None :
428
+ return STATE_UNAVAILABLE
429
+ if power :
430
+ return STATE_ON
431
+ else :
432
+ return STATE_OFF
433
+
434
+ async def async_turn_on (self ):
435
+ """Turn on master power for the J-Tech Digital HDMI Matrix."""
436
+ await self ._coordinator .async_power_on ()
437
+ await self ._coordinator .async_request_refresh ()
438
+
439
+ async def async_turn_off (self ):
440
+ """Turn off master power for the J-Tech Digital HDMI Matrix."""
441
+ await self ._coordinator .async_power_off ()
442
+ await self ._coordinator .async_request_refresh ()
443
+
444
+ @callback
445
+ async def _handle_coordinator_update (self ) -> None :
446
+ """Handle updated data from the coordinator."""
447
+ # Trigger an entity state update to reflect the latest data from the coordinator
448
+ _LOGGER .debug ("handle_coordinator_update_data" , self ._coordinator .data )
449
+ self .async_write_ha_state ()
0 commit comments