Skip to content

Commit

Permalink
Query device settings, expose device entry, proper unique ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Feb 7, 2025
1 parent fecf575 commit 874ddd4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
15 changes: 13 additions & 2 deletions custom_components/atenedgar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import logging

from attr import dataclass
from homeassistant import config_entries
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

from custom_components.atenedgar.aten import Aten
from custom_components.atenedgar.edgar import Edgar
from custom_components.atenedgar.edgar import Edgar, EdgarSettings


@dataclass
class AtenEdgarConfigEntryRuntimeData:
aten: Aten
edgar_settings: EdgarSettings


logger = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: config_entries.ConfigEntry):
Expand All @@ -22,9 +28,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: config_entries.ConfigEnt
if not await edgar.is_reachable():
raise ConfigEntryNotReady("Unable to connect")

# Query Edgar settings so we can build device information
settings = await edgar.get_settings()
if settings is None:
raise ConfigEntryNotReady("Unable to fetch device settings")

# Create device
aten = Aten(edgar, entry.data["name"])
entry.runtime_data = AtenEdgarConfigEntryRuntimeData(aten)
entry.runtime_data = AtenEdgarConfigEntryRuntimeData(aten, settings)

hass.async_create_task(hass.config_entries.async_forward_entry_setups(entry, [Platform.MEDIA_PLAYER]))
return True
Expand Down
22 changes: 18 additions & 4 deletions custom_components/atenedgar/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
MediaPlayerState,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.device_registry import DeviceInfo, format_mac

from . import Aten, AtenEdgarConfigEntryRuntimeData
from . import Aten, AtenEdgarConfigEntryRuntimeData, EdgarSettings
from .const import DOMAIN

logger = logging.getLogger(__name__)

Expand All @@ -17,12 +19,13 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities):
"""Set up the HDMI Switch media player from a config entry."""
runtime_data: AtenEdgarConfigEntryRuntimeData = entry.runtime_data

async_add_entities([AtenHDMISwitch(runtime_data.aten)])
async_add_entities([AtenHDMISwitch(runtime_data.aten, runtime_data.edgar_settings)])


class AtenHDMISwitch(MediaPlayerEntity):
def __init__(self, aten: Aten):
def __init__(self, aten: Aten, edgar_settings: EdgarSettings):
self._aten = aten
self._edgar_settings = edgar_settings

self._device_class = MediaPlayerDeviceClass.RECEIVER
self._state = MediaPlayerState.PLAYING
Expand All @@ -35,9 +38,20 @@ def __init__(self, aten: Aten):
def device_class(self) -> MediaPlayerDeviceClass:
return self._device_class

@property
def device_info(self) -> DeviceInfo:
return DeviceInfo(
identifiers={(DOMAIN, format_mac(self._edgar_settings.mac))},
configuration_url=f"http://{self._edgar_settings.ip}/",
manufacturer="Papouch",
model=self._edgar_settings.type,
sw_version=self._edgar_settings.fw,
)

@property
def unique_id(self) -> str:
return "aten_hdmi_switch"
mac = format_mac(self._edgar_settings.mac)
return f"aten_hdmi_switch_via_edgar_{mac}"

@property
def state(self) -> MediaPlayerState:
Expand Down

0 comments on commit 874ddd4

Please sign in to comment.