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 )
0 commit comments