Skip to content

Commit cd34b17

Browse files
committed
create device
1 parent 8e52e8c commit cd34b17

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

custom_components/hyperhdr_control/__init__.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,38 @@
33

44
from homeassistant.config_entries import ConfigEntry
55
from homeassistant.core import HomeAssistant
6-
from homeassistant.const import Platform
6+
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
7+
from homeassistant.helpers import device_registry as dr
8+
9+
from .const import DOMAIN
710

811
PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.BUTTON]
912

1013
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1114
"""Set up HyperHDR Control from a config entry."""
15+
# Store an instance of the "domain" that includes the host/port
16+
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
17+
"host": entry.data[CONF_HOST],
18+
"port": entry.data[CONF_PORT],
19+
}
20+
21+
# Register device
22+
device_registry = dr.async_get(hass)
23+
device_registry.async_get_or_create(
24+
config_entry_id=entry.entry_id,
25+
identifiers={(DOMAIN, f"{entry.data[CONF_HOST]}:{entry.data[CONF_PORT]}")},
26+
manufacturer="HyperHDR",
27+
name=f"HyperHDR ({entry.data[CONF_HOST]})",
28+
model="HyperHDR LED Controller",
29+
sw_version="1.0.0",
30+
)
31+
1232
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
1333
return True
1434

1535
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1636
"""Unload a config entry."""
17-
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
37+
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
38+
if unload_ok:
39+
hass.data[DOMAIN].pop(entry.entry_id)
40+
return unload_ok

custom_components/hyperhdr_control/button.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
from homeassistant.config_entries import ConfigEntry
1212
from homeassistant.const import CONF_HOST, CONF_PORT
1313
from homeassistant.core import HomeAssistant
14+
from homeassistant.helpers.entity import DeviceInfo
1415
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1516

16-
from .const import AVAILABLE_EFFECTS
17+
from .const import DOMAIN, AVAILABLE_EFFECTS
1718

1819
_LOGGER = logging.getLogger(__name__)
1920

@@ -28,21 +29,33 @@ async def async_setup_entry(
2829

2930
entities = []
3031
for effect in AVAILABLE_EFFECTS:
31-
entities.append(HyperHDREffectButton(host, port, effect))
32+
entities.append(HyperHDREffectButton(entry.entry_id, host, port, effect))
3233

3334
async_add_entities(entities, True)
3435

3536
class HyperHDREffectButton(ButtonEntity):
3637
"""Representation of a HyperHDR Effect button."""
3738

38-
def __init__(self, host: str, port: int, effect_name: str) -> None:
39+
def __init__(self, entry_id: str, host: str, port: int, effect_name: str) -> None:
3940
"""Initialize the button."""
41+
self._entry_id = entry_id
4042
self._host = host
4143
self._port = port
4244
self._effect_name = effect_name
4345
self._attr_name = f"HyperHDR Effect {effect_name}"
4446
self._attr_unique_id = f"hyperhdr_effect_{host}_{port}_{effect_name.lower().replace(' ', '_')}"
4547

48+
@property
49+
def device_info(self) -> DeviceInfo:
50+
"""Return device information about this HyperHDR instance."""
51+
return DeviceInfo(
52+
identifiers={(DOMAIN, f"{self._host}:{self._port}")},
53+
manufacturer="HyperHDR",
54+
name=f"HyperHDR ({self._host})",
55+
model="HyperHDR LED Controller",
56+
sw_version="1.0.0",
57+
)
58+
4659
async def async_press(self) -> None:
4760
"""Handle the button press."""
4861
request_data = {

custom_components/hyperhdr_control/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"codeowners": [],
77
"requirements": ["aiohttp"],
88
"iot_class": "local_polling",
9-
"version": "1.0.0",
9+
"version": "1.0.1",
1010
"config_flow": true
1111
}

custom_components/hyperhdr_control/switch.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
from homeassistant.config_entries import ConfigEntry
1212
from homeassistant.const import CONF_HOST, CONF_PORT
1313
from homeassistant.core import HomeAssistant
14+
from homeassistant.helpers.entity import DeviceInfo
1415
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1516

17+
from .const import DOMAIN
18+
1619
_LOGGER = logging.getLogger(__name__)
1720

1821
async def async_setup_entry(
@@ -24,19 +27,31 @@ async def async_setup_entry(
2427
host = entry.data[CONF_HOST]
2528
port = entry.data[CONF_PORT]
2629

27-
async_add_entities([HyperHDRSwitch(host, port)], True)
30+
async_add_entities([HyperHDRSwitch(entry.entry_id, host, port)], True)
2831

2932
class HyperHDRSwitch(SwitchEntity):
3033
"""Representation of a HyperHDR Control switch."""
3134

32-
def __init__(self, host: str, port: int) -> None:
35+
def __init__(self, entry_id: str, host: str, port: int) -> None:
3336
"""Initialize the switch."""
37+
self._entry_id = entry_id
3438
self._host = host
3539
self._port = port
3640
self._attr_name = "HyperHDR USB Capture"
3741
self._attr_unique_id = f"hyperhdr_videograbber_{host}_{port}"
3842
self._attr_is_on = False
3943

44+
@property
45+
def device_info(self) -> DeviceInfo:
46+
"""Return device information about this HyperHDR instance."""
47+
return DeviceInfo(
48+
identifiers={(DOMAIN, f"{self._host}:{self._port}")},
49+
manufacturer="HyperHDR",
50+
name=f"HyperHDR ({self._host})",
51+
model="HyperHDR LED Controller",
52+
sw_version="1.0.0",
53+
)
54+
4055
async def async_turn_on(self, **kwargs: Any) -> None:
4156
"""Turn the entity on."""
4257
await self._set_state(True)

0 commit comments

Comments
 (0)