1 """Switch platform for Hyperion."""
3 from __future__
import annotations
8 from hyperion
import client
9 from hyperion.const
import (
12 KEY_COMPONENTID_BLACKBORDER,
13 KEY_COMPONENTID_BOBLIGHTSERVER,
14 KEY_COMPONENTID_FORWARDER,
15 KEY_COMPONENTID_GRABBER,
16 KEY_COMPONENTID_LEDDEVICE,
17 KEY_COMPONENTID_SMOOTHING,
18 KEY_COMPONENTID_TO_NAME,
34 async_dispatcher_connect,
35 async_dispatcher_send,
41 get_hyperion_device_id,
42 get_hyperion_unique_id,
43 listen_for_instance_updates,
46 CONF_INSTANCE_CLIENTS,
48 HYPERION_MANUFACTURER_NAME,
51 TYPE_HYPERION_COMPONENT_SWITCH_BASE,
54 COMPONENT_SWITCHES = [
56 KEY_COMPONENTID_SMOOTHING,
57 KEY_COMPONENTID_BLACKBORDER,
58 KEY_COMPONENTID_FORWARDER,
59 KEY_COMPONENTID_BOBLIGHTSERVER,
60 KEY_COMPONENTID_GRABBER,
61 KEY_COMPONENTID_LEDDEVICE,
67 """Convert a component to a unique_id."""
72 f
"{TYPE_HYPERION_COMPONENT_SWITCH_BASE} {KEY_COMPONENTID_TO_NAME[component]}"
79 KEY_COMPONENTID_ALL:
"all",
80 KEY_COMPONENTID_SMOOTHING:
"smoothing",
81 KEY_COMPONENTID_BLACKBORDER:
"blackbar_detection",
82 KEY_COMPONENTID_FORWARDER:
"forwarder",
83 KEY_COMPONENTID_BOBLIGHTSERVER:
"boblight_server",
84 KEY_COMPONENTID_GRABBER:
"platform_capture",
85 KEY_COMPONENTID_LEDDEVICE:
"led_device",
86 KEY_COMPONENTID_V4L:
"usb_capture",
92 config_entry: ConfigEntry,
93 async_add_entities: AddEntitiesCallback,
95 """Set up a Hyperion platform from config entry."""
96 entry_data = hass.data[DOMAIN][config_entry.entry_id]
97 server_id = config_entry.unique_id
100 def instance_add(instance_num: int, instance_name: str) ->
None:
101 """Add entities for a new Hyperion instance."""
109 entry_data[CONF_INSTANCE_CLIENTS][instance_num],
111 for component
in COMPONENT_SWITCHES
115 def instance_remove(instance_num: int) ->
None:
116 """Remove entities for an old Hyperion instance."""
118 for component
in COMPONENT_SWITCHES:
121 SIGNAL_ENTITY_REMOVE.format(
130 """ComponentBinarySwitch switch class."""
132 _attr_entity_category = EntityCategory.CONFIG
133 _attr_should_poll =
False
134 _attr_has_entity_name =
True
136 _attr_entity_registry_enabled_default =
False
144 hyperion_client: client.HyperionClient,
146 """Initialize the switch."""
148 server_id, component_name, instance_num
159 identifiers={(DOMAIN, self.
_device_id_device_id)},
160 manufacturer=HYPERION_MANUFACTURER_NAME,
161 model=HYPERION_MODEL_NAME,
163 configuration_url=self.
_client_client.remote_url,
168 """Return true if the switch is on."""
169 for component
in self.
_client_client.components
or []:
171 return bool(component.setdefault(KEY_ENABLED,
False))
176 """Return server availability."""
180 """Send a component control request."""
181 await self.
_client_client.async_send_set_component(
183 KEY_COMPONENTSTATE: {
191 """Turn on the switch."""
195 """Turn off the switch."""
200 """Update Hyperion components."""
204 """Register callbacks when entity added to hass."""
209 functools.partial(self.
async_removeasync_remove, force_remove=
True),
216 """Cleanup prior to hass removal."""
None async_turn_on(self, **Any kwargs)
None async_added_to_hass(self)
None async_will_remove_from_hass(self)
None async_turn_off(self, **Any kwargs)
None __init__(self, str server_id, int instance_num, str instance_name, str component_name, client.HyperionClient hyperion_client)
None _update_components(self, dict[str, Any]|None _=None)
None _async_send_set_component(self, bool value)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_remove(self, *bool force_remove=False)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
str _component_to_translation_key(str component)
str _component_to_unique_id(str server_id, str component, int instance_num)
str get_hyperion_device_id(str server_id, int instance)
str get_hyperion_unique_id(str server_id, int instance, str name)
None listen_for_instance_updates(HomeAssistant hass, ConfigEntry config_entry, Callable add_func, Callable remove_func)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)