Skip to content

Commit b836a29

Browse files
authored
Merge pull request #7 from ejochman/main
@ejochman suggested version
2 parents ba5eb68 + e68cfc9 commit b836a29

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

custom_components/jtechdigital/coordinator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,9 @@ async def async_send_cec_output(self, output: int, command: int) -> bool:
200200
async def async_send_cec_source(self, source: int, command: int) -> bool:
201201
"""Send a CEC command to the specified source."""
202202
return await self._client.send_cec_source(source, command)
203+
204+
async def async_power_on(self) -> bool:
205+
return await self._client.set_power(True)
206+
207+
async def async_power_off(self) -> bool:
208+
return await self._client.set_power(False)

custom_components/jtechdigital/media_player.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from homeassistant.components.media_player import (
77
MediaPlayerDeviceClass,
88
MediaPlayerEntity,
9+
MediaPlayerEntityFeature,
910
SUPPORT_PAUSE,
1011
SUPPORT_PLAY,
1112
SUPPORT_STOP,
@@ -77,6 +78,8 @@ async def async_setup_entry(
7778
for output_idx, output_info in enumerate(coordinator.outputs)
7879
]
7980

81+
entities.append(JtechMasterMediaPlayer(config_entry, coordinator))
82+
8083
# Add the media player entities to Home Assistant
8184
async_add_entities(entities, update_before_add=True)
8285

@@ -194,7 +197,7 @@ def device_info(self) -> DeviceInfo:
194197

195198
return DeviceInfo(
196199
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}",
198201
manufacturer=ATTR_MANUFACTURER,
199202
model=self._coordinator.data["model"],
200203
sw_version=self._coordinator.data["version"],
@@ -246,6 +249,7 @@ async def async_select_source(self, source):
246249
if source_info.name == source:
247250
await self._coordinator.async_select_source(self._output_index, source_idx + 1)
248251
break
252+
await self._coordinator.async_request_refresh()
249253

250254
async def async_turn_on(self):
251255
"""Enable the output and send necessary CEC commands to turn on the connected devices."""
@@ -272,6 +276,7 @@ async def async_turn_on(self):
272276
if delay_source and delay_source > 0:
273277
await asyncio.sleep(delay_source)
274278
await self._coordinator.async_send_cec_output(self._output_index, 5)
279+
await self._coordinator.async_request_refresh()
275280

276281
async def async_turn_off(self):
277282
"""Disable the output."""
@@ -292,6 +297,7 @@ async def async_turn_off(self):
292297
await self._coordinator.async_disable_output(self._output_index)
293298
if cat_stream_toggle:
294299
await self._coordinator.async_disable_cat_output(self._output_index)
300+
await self._coordinator.async_request_refresh()
295301

296302

297303
async def async_volume_up(self):
@@ -373,3 +379,71 @@ async def _handle_coordinator_update(self) -> None:
373379
# Trigger an entity state update to reflect the latest data from the coordinator
374380
_LOGGER.debug("handle_coordinator_update_data", self._coordinator.data)
375381
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

Comments
 (0)