Skip to content

Commit

Permalink
introduce via_device
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 committed Sep 22, 2023
1 parent fbc73c7 commit bf4c556
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/comelit/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
_LOGGER = logging.getLogger(__package__)

DOMAIN = "comelit"
BRIDGE_MODEL = "Serial bridge"
51 changes: 34 additions & 17 deletions homeassistant/components/comelit/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import timedelta
from typing import Any

from aiocomelit import ComeliteSerialBridgeAPi
from aiocomelit import ComeliteSerialBridgeAPi, ComelitSerialBridgeObject
import aiohttp

from homeassistant.config_entries import ConfigEntry
Expand All @@ -12,7 +12,7 @@
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import _LOGGER, DOMAIN
from .const import _LOGGER, BRIDGE_MODEL, DOMAIN


class ComelitSerialBridge(DataUpdateCoordinator):
Expand All @@ -34,6 +34,38 @@ def __init__(self, hass: HomeAssistant, host: str, pin: int) -> None:
name=f"{DOMAIN}-{host}-coordinator",
update_interval=timedelta(seconds=5),
)
device_registry = dr.async_get(self.hass)
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id,
identifiers={(DOMAIN, self.config_entry.entry_id)},
model=BRIDGE_MODEL,
name=f"{BRIDGE_MODEL} ({self.api.host})",
**self.basic_device_info,
)

@property
def basic_device_info(self) -> dict:
"""Set basic device info."""

return {
"manufacturer": "Comelit",
"hw_version": "20003101",
}

def platform_device_info(
self, device: ComelitSerialBridgeObject, platform: str
) -> dr.DeviceInfo:
"""Set platform device info."""

return dr.DeviceInfo(
identifiers={
(DOMAIN, f"{self.config_entry.entry_id}-{platform}-{device.index}")
},
via_device=(DOMAIN, self.config_entry.entry_id),
name=device.name,
model=f"{BRIDGE_MODEL} {platform}",
**self.basic_device_info,
)

async def _async_update_data(self) -> dict[str, Any]:
"""Update router data."""
Expand All @@ -51,18 +83,3 @@ async def _async_update_data(self) -> dict[str, Any]:
await self.api.logout()

return devices_data

def register_device(self) -> None:
"""Create device with all available info."""

device_registry = dr.async_get(self.hass)
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id,
identifiers={
(DOMAIN, self.config_entry.entry_id),
},
manufacturer="Comelit",
model="Serial Bridge",
hw_version="20003101",
name=f"Serial Bridge ({self.api.host})",
)
4 changes: 2 additions & 2 deletions homeassistant/components/comelit/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ def __init__(
self,
coordinator: ComelitSerialBridge,
device: ComelitSerialBridgeObject,
config_entry_unique_id: str | None,
config_entry_unique_id: str,
) -> None:
"""Init light entity."""
self._api = coordinator.api
self._device = device
super().__init__(coordinator)
self._attr_name = device.name
self._attr_unique_id = f"{config_entry_unique_id}-{device.index}"
self.coordinator.register_device()
self._attr_device_info = self.coordinator.platform_device_info(device, LIGHT)

async def _light_set_state(self, state: int) -> None:
"""Set desired light state."""
Expand Down

0 comments on commit bf4c556

Please sign in to comment.