47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""Light support for switch entities."""
|
|
from __future__ import annotations
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.light import PLATFORM_SCHEMA
|
|
from homeassistant.components.switch_as_x import LightSwitch
|
|
from homeassistant.const import CONF_ENTITY_ID, CONF_NAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import entity_registry as er
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
from .const import DOMAIN as SWITCH_DOMAIN
|
|
|
|
DEFAULT_NAME = "Light Switch"
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
{
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_domain(SWITCH_DOMAIN),
|
|
}
|
|
)
|
|
|
|
|
|
async def async_setup_platform(
|
|
hass: HomeAssistant,
|
|
config: ConfigType,
|
|
async_add_entities: AddEntitiesCallback,
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
) -> None:
|
|
"""Initialize Light Switch platform."""
|
|
registry = er.async_get(hass)
|
|
wrapped_switch = registry.async_get(config[CONF_ENTITY_ID])
|
|
unique_id = wrapped_switch.unique_id if wrapped_switch else None
|
|
|
|
async_add_entities(
|
|
[
|
|
LightSwitch(
|
|
config[CONF_NAME],
|
|
config[CONF_ENTITY_ID],
|
|
unique_id,
|
|
)
|
|
]
|
|
)
|