Skip to content

Commit 9a822f0

Browse files
committed
added led device and brightness control
1 parent cd34b17 commit 9a822f0

File tree

4 files changed

+115
-12
lines changed

4 files changed

+115
-12
lines changed

custom_components/hyperhdr_control/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .const import DOMAIN
1010

11-
PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.BUTTON]
11+
PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.BUTTON, Platform.NUMBER]
1212

1313
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1414
"""Set up HyperHDR Control from a config entry."""
@@ -26,7 +26,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2626
manufacturer="HyperHDR",
2727
name=f"HyperHDR ({entry.data[CONF_HOST]})",
2828
model="HyperHDR LED Controller",
29-
sw_version="1.0.0",
29+
sw_version="1.1.0",
3030
)
3131

3232
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

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.1",
9+
"version": "1.1.0",
1010
"config_flow": true
1111
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""Number platform for HyperHDR Control integration."""
2+
from __future__ import annotations
3+
4+
import json
5+
import logging
6+
from typing import Any
7+
8+
import aiohttp
9+
import async_timeout
10+
from homeassistant.components.number import NumberEntity, NumberMode
11+
from homeassistant.config_entries import ConfigEntry
12+
from homeassistant.const import CONF_HOST, CONF_PORT, PERCENTAGE
13+
from homeassistant.core import HomeAssistant
14+
from homeassistant.helpers.entity import DeviceInfo
15+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
16+
17+
from .const import DOMAIN
18+
19+
_LOGGER = logging.getLogger(__name__)
20+
21+
async def async_setup_entry(
22+
hass: HomeAssistant,
23+
entry: ConfigEntry,
24+
async_add_entities: AddEntitiesCallback,
25+
) -> None:
26+
"""Set up the HyperHDR Control number entities."""
27+
host = entry.data[CONF_HOST]
28+
port = entry.data[CONF_PORT]
29+
30+
async_add_entities([
31+
HyperHDRBrightnessNumber(entry.entry_id, host, port)
32+
], True)
33+
34+
class HyperHDRBrightnessNumber(NumberEntity):
35+
"""Representation of a HyperHDR brightness control."""
36+
37+
def __init__(self, entry_id: str, host: str, port: int) -> None:
38+
"""Initialize the number entity."""
39+
self._entry_id = entry_id
40+
self._host = host
41+
self._port = port
42+
self._attr_name = "HyperHDR Brightness"
43+
self._attr_unique_id = f"hyperhdr_brightness_{host}_{port}"
44+
self._attr_native_min_value = 0
45+
self._attr_native_max_value = 100
46+
self._attr_native_step = 1
47+
self._attr_mode = NumberMode.SLIDER
48+
self._attr_native_unit_of_measurement = PERCENTAGE
49+
50+
@property
51+
def device_info(self) -> DeviceInfo:
52+
"""Return device information about this HyperHDR instance."""
53+
return DeviceInfo(
54+
identifiers={(DOMAIN, f"{self._host}:{self._port}")},
55+
manufacturer="HyperHDR",
56+
name=f"HyperHDR ({self._host})",
57+
model="HyperHDR LED Controller",
58+
sw_version="1.1.0",
59+
)
60+
61+
async def async_set_native_value(self, value: float) -> None:
62+
"""Set the brightness value."""
63+
request_data = {
64+
"command": "adjustment",
65+
"adjustment": {
66+
"classic_config": False,
67+
"brightness": int(value)
68+
}
69+
}
70+
71+
try:
72+
async with aiohttp.ClientSession() as session:
73+
url = f"http://{self._host}:{self._port}/json-rpc"
74+
async with async_timeout.timeout(10):
75+
async with session.get(url, params={"request": json.dumps(request_data)}) as response:
76+
if response.status == 200:
77+
self._attr_native_value = value
78+
else:
79+
_LOGGER.error("Failed to set brightness: %s", response.status)
80+
except (aiohttp.ClientError, TimeoutError) as error:
81+
_LOGGER.error("Error setting brightness: %s", error)
82+
83+
async def async_update(self) -> None:
84+
"""Update the current brightness value."""
85+
request_data = {
86+
"command": "serverinfo"
87+
}
88+
89+
try:
90+
async with aiohttp.ClientSession() as session:
91+
url = f"http://{self._host}:{self._port}/json-rpc"
92+
async with async_timeout.timeout(10):
93+
async with session.get(url, params={"request": json.dumps(request_data)}) as response:
94+
if response.status == 200:
95+
data = await response.json()
96+
adjustment = data.get("info", {}).get("adjustment", {})
97+
self._attr_native_value = float(adjustment.get("brightness", 100))
98+
except (aiohttp.ClientError, TimeoutError) as error:
99+
_LOGGER.error("Error updating brightness: %s", error)

custom_components/hyperhdr_control/switch.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,26 @@ async def async_setup_entry(
2323
entry: ConfigEntry,
2424
async_add_entities: AddEntitiesCallback,
2525
) -> None:
26-
"""Set up the HyperHDR Control switch."""
26+
"""Set up the HyperHDR Control switches."""
2727
host = entry.data[CONF_HOST]
2828
port = entry.data[CONF_PORT]
2929

30-
async_add_entities([HyperHDRSwitch(entry.entry_id, host, port)], True)
30+
async_add_entities([
31+
HyperHDRSwitch(entry.entry_id, host, port, "VIDEOGRABBER", "USB Capture"),
32+
HyperHDRSwitch(entry.entry_id, host, port, "LEDDEVICE", "LED Output")
33+
], True)
3134

3235
class HyperHDRSwitch(SwitchEntity):
3336
"""Representation of a HyperHDR Control switch."""
3437

35-
def __init__(self, entry_id: str, host: str, port: int) -> None:
38+
def __init__(self, entry_id: str, host: str, port: int, component: str, name: str) -> None:
3639
"""Initialize the switch."""
3740
self._entry_id = entry_id
3841
self._host = host
3942
self._port = port
40-
self._attr_name = "HyperHDR USB Capture"
41-
self._attr_unique_id = f"hyperhdr_videograbber_{host}_{port}"
43+
self._component = component
44+
self._attr_name = f"HyperHDR {name}"
45+
self._attr_unique_id = f"hyperhdr_{component.lower()}_{host}_{port}"
4246
self._attr_is_on = False
4347

4448
@property
@@ -49,7 +53,7 @@ def device_info(self) -> DeviceInfo:
4953
manufacturer="HyperHDR",
5054
name=f"HyperHDR ({self._host})",
5155
model="HyperHDR LED Controller",
52-
sw_version="1.0.0",
56+
sw_version="1.1.0",
5357
)
5458

5559
async def async_turn_on(self, **kwargs: Any) -> None:
@@ -61,11 +65,11 @@ async def async_turn_off(self, **kwargs: Any) -> None:
6165
await self._set_state(False)
6266

6367
async def _set_state(self, state: bool) -> None:
64-
"""Set the state of the video grabber."""
68+
"""Set the state of the component."""
6569
request_data = {
6670
"command": "componentstate",
6771
"componentstate": {
68-
"component": "VIDEOGRABBER",
72+
"component": self._component,
6973
"state": state
7074
}
7175
}
@@ -97,7 +101,7 @@ async def async_update(self) -> None:
97101
data = await response.json()
98102
components = data.get("info", {}).get("components", [])
99103
for component in components:
100-
if component.get("name") == "VIDEOGRABBER":
104+
if component.get("name") == self._component:
101105
self._attr_is_on = component.get("enabled", False)
102106
break
103107
except (aiohttp.ClientError, TimeoutError) as error:

0 commit comments

Comments
 (0)