1111
1212from .bravia_client import BraviaError
1313from .const import (
14+ BRIGHTNESS_PRESETS ,
1415 DEFAULT_PICTURE_MODES ,
1516 DOMAIN ,
1617 SCREEN_ROTATION_OPTIONS ,
@@ -30,14 +31,15 @@ async def async_setup_entry(
3031) -> None :
3132 """Set up Bravia REST API select entities."""
3233 coordinator : BraviaCoordinator = hass .data [DOMAIN ][entry .entry_id ]
33- async_add_entities (
34- [
35- BraviaSoundOutputSelect (coordinator , entry ),
36- BraviaScreenRotationSelect (coordinator , entry ),
37- BraviaPictureModeSelect (coordinator , entry ),
38- BraviaSleepTimerSelect (coordinator , entry ),
39- ]
40- )
34+ entities : list [SelectEntity ] = [
35+ BraviaSoundOutputSelect (coordinator , entry ),
36+ BraviaScreenRotationSelect (coordinator , entry ),
37+ BraviaPictureModeSelect (coordinator , entry ),
38+ BraviaSleepTimerSelect (coordinator , entry ),
39+ ]
40+ if coordinator .brightness_supported :
41+ entities .append (BraviaBrightnessSelect (coordinator , entry ))
42+ async_add_entities (entities )
4143
4244
4345class BraviaSoundOutputSelect (BraviaEntity , SelectEntity ):
@@ -247,3 +249,58 @@ async def async_added_to_hass(self) -> None:
247249 break
248250 except BraviaError :
249251 pass
252+
253+
254+ class BraviaBrightnessSelect (BraviaEntity , SelectEntity ):
255+ """Select entity for brightness presets."""
256+
257+ _attr_translation_key = "brightness_preset"
258+ _attr_icon = "mdi:brightness-6"
259+
260+ def __init__ (
261+ self ,
262+ coordinator : BraviaCoordinator ,
263+ entry : ConfigEntry ,
264+ ) -> None :
265+ super ().__init__ (coordinator , entry )
266+ self ._attr_unique_id = f"{ entry .unique_id } _brightness_preset"
267+ self ._attr_options = list (BRIGHTNESS_PRESETS .keys ())
268+ self ._bri_min = coordinator .brightness_min
269+ self ._bri_max = coordinator .brightness_max
270+
271+ def _value_for_preset (self , preset : str ) -> int :
272+ """Calculate brightness value for a preset name."""
273+ fraction = BRIGHTNESS_PRESETS [preset ]
274+ return round (self ._bri_min + fraction * (self ._bri_max - self ._bri_min ))
275+
276+ @property
277+ def current_option (self ) -> str | None :
278+ """Return the closest brightness preset to the current value."""
279+ data = self .coordinator .data
280+ if not data or data .brightness is None :
281+ return None
282+ current = data .brightness
283+ bri_range = self ._bri_max - self ._bri_min
284+ if bri_range == 0 :
285+ return "Min"
286+ fraction = (current - self ._bri_min ) / bri_range
287+ closest = "Medium"
288+ closest_dist = float ("inf" )
289+ for name , pct in BRIGHTNESS_PRESETS .items ():
290+ dist = abs (fraction - pct )
291+ if dist < closest_dist :
292+ closest_dist = dist
293+ closest = name
294+ return closest
295+
296+ async def async_select_option (self , option : str ) -> None :
297+ """Set brightness to a preset value."""
298+ if option not in BRIGHTNESS_PRESETS :
299+ _LOGGER .error ("Unknown brightness preset: %s" , option )
300+ return
301+ value = self ._value_for_preset (option )
302+ try :
303+ await self .coordinator .client .set_brightness (value )
304+ except BraviaError as err :
305+ _LOGGER .error ("Failed to set brightness preset: %s" , err )
306+ await self .coordinator .async_request_refresh ()
0 commit comments