Add service to group switches (#998)

This commit is contained in:
Björn Ebbinghaus 2024-05-15 18:07:59 +02:00 committed by GitHub
commit 618e2ccf4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -54,6 +54,8 @@ from homeassistant.const import (
EVENT_CALL_SERVICE,
EVENT_HOMEASSISTANT_STARTED,
EVENT_STATE_CHANGED,
MAJOR_VERSION,
MINOR_VERSION,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
@ -70,6 +72,12 @@ from homeassistant.core import (
callback,
)
from homeassistant.helpers import entity_platform, entity_registry
if [MAJOR_VERSION, MINOR_VERSION] < [2023, 9]:
from homeassistant.helpers.entity import DeviceInfo
else:
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.event import (
async_track_state_change_event,
async_track_time_interval,
@ -944,6 +952,17 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
"""Return true if adaptive lighting is on."""
return self._state
@property
def device_info(self) -> DeviceInfo:
"""Return the device info, used to group this and adjacent entities in the UI."""
return DeviceInfo(
identifiers={
(DOMAIN, self._name),
},
name=self._name,
entry_type=DeviceEntryType.SERVICE,
)
async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to hass."""
if self.hass.is_running:
@ -1563,9 +1582,9 @@ class SimpleSwitch(SwitchEntity, RestoreEntity):
self._icon = icon
self._state: bool | None = None
self._which = which
name = data[CONF_NAME]
self._unique_id = f"{name}_{slugify(self._which)}"
self._name = f"Adaptive Lighting {which}: {name}"
self._config_name = data[CONF_NAME]
self._unique_id = f"{self._config_name}_{slugify(self._which)}"
self._name = f"Adaptive Lighting {which}: {self._config_name}"
self._initial_state = initial_state
@property
@ -1588,6 +1607,17 @@ class SimpleSwitch(SwitchEntity, RestoreEntity):
"""Return true if adaptive lighting is on."""
return self._state
@property
def device_info(self) -> DeviceInfo:
"""Return the device info, used to group this and adjacent entities in the UI."""
return DeviceInfo(
identifiers={
(DOMAIN, self._config_name),
},
name=f"Adaptive Lighting: {self._config_name}",
entry_type=DeviceEntryType.SERVICE,
)
async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to hass."""
last_state = await self.async_get_last_state()