mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Cleanup switch light (#63127)
This commit is contained in:
parent
78442d82d1
commit
c5d8792c34
@ -1,7 +1,7 @@
|
|||||||
"""Light support for switch entities."""
|
"""Light support for switch entities."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, cast
|
from typing import Any
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -18,14 +18,13 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, State, callback
|
from homeassistant.core import Event, HomeAssistant, callback
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
|
|
||||||
|
|
||||||
DEFAULT_NAME = "Light Switch"
|
DEFAULT_NAME = "Light Switch"
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
@ -43,15 +42,14 @@ async def async_setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Light Switch platform."""
|
"""Initialize Light Switch platform."""
|
||||||
|
registry = er.async_get(hass)
|
||||||
registry = await hass.helpers.entity_registry.async_get_registry()
|
|
||||||
wrapped_switch = registry.async_get(config[CONF_ENTITY_ID])
|
wrapped_switch = registry.async_get(config[CONF_ENTITY_ID])
|
||||||
unique_id = wrapped_switch.unique_id if wrapped_switch else None
|
unique_id = wrapped_switch.unique_id if wrapped_switch else None
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
LightSwitch(
|
LightSwitch(
|
||||||
cast(str, config.get(CONF_NAME)),
|
config[CONF_NAME],
|
||||||
config[CONF_ENTITY_ID],
|
config[CONF_ENTITY_ID],
|
||||||
unique_id,
|
unique_id,
|
||||||
)
|
)
|
||||||
@ -62,74 +60,50 @@ async def async_setup_platform(
|
|||||||
class LightSwitch(LightEntity):
|
class LightSwitch(LightEntity):
|
||||||
"""Represents a Switch as a Light."""
|
"""Represents a Switch as a Light."""
|
||||||
|
|
||||||
def __init__(self, name: str, switch_entity_id: str, unique_id: str) -> None:
|
_attr_color_mode = COLOR_MODE_ONOFF
|
||||||
|
_attr_should_poll = False
|
||||||
|
_attr_supported_color_modes = {COLOR_MODE_ONOFF}
|
||||||
|
|
||||||
|
def __init__(self, name: str, switch_entity_id: str, unique_id: str | None) -> None:
|
||||||
"""Initialize Light Switch."""
|
"""Initialize Light Switch."""
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
|
self._attr_unique_id = unique_id
|
||||||
self._switch_entity_id = switch_entity_id
|
self._switch_entity_id = switch_entity_id
|
||||||
self._unique_id = unique_id
|
|
||||||
self._switch_state: State | None = None
|
|
||||||
self._attr_color_mode = COLOR_MODE_ONOFF
|
|
||||||
self._attr_supported_color_modes = {COLOR_MODE_ONOFF}
|
|
||||||
|
|
||||||
@property
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
def name(self) -> str:
|
|
||||||
"""Return the name of the entity."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool:
|
|
||||||
"""Return true if light switch is on."""
|
|
||||||
assert self._switch_state is not None
|
|
||||||
return self._switch_state.state == STATE_ON
|
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return true if light switch is on."""
|
|
||||||
return (
|
|
||||||
self._switch_state is not None
|
|
||||||
and self._switch_state.state != STATE_UNAVAILABLE
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""No polling needed for a light switch."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique id of the light switch."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
|
||||||
"""Forward the turn_on command to the switch in this light switch."""
|
"""Forward the turn_on command to the switch in this light switch."""
|
||||||
data = {ATTR_ENTITY_ID: self._switch_entity_id}
|
|
||||||
await self.hass.services.async_call(
|
await self.hass.services.async_call(
|
||||||
switch.DOMAIN,
|
switch.DOMAIN,
|
||||||
switch.SERVICE_TURN_ON,
|
switch.SERVICE_TURN_ON,
|
||||||
data,
|
{ATTR_ENTITY_ID: self._switch_entity_id},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
context=self._context,
|
context=self._context,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Forward the turn_off command to the switch in this light switch."""
|
"""Forward the turn_off command to the switch in this light switch."""
|
||||||
data = {ATTR_ENTITY_ID: self._switch_entity_id}
|
|
||||||
await self.hass.services.async_call(
|
await self.hass.services.async_call(
|
||||||
switch.DOMAIN,
|
switch.DOMAIN,
|
||||||
switch.SERVICE_TURN_OFF,
|
switch.SERVICE_TURN_OFF,
|
||||||
data,
|
{ATTR_ENTITY_ID: self._switch_entity_id},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
context=self._context,
|
context=self._context,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
self._switch_state = self.hass.states.get(self._switch_entity_id)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_state_changed_listener(*_: Any) -> None:
|
def async_state_changed_listener(event: Event | None = None) -> None:
|
||||||
"""Handle child updates."""
|
"""Handle child updates."""
|
||||||
self._switch_state = self.hass.states.get(self._switch_entity_id)
|
if (
|
||||||
|
state := self.hass.states.get(self._switch_entity_id)
|
||||||
|
) is None or state.state == STATE_UNAVAILABLE:
|
||||||
|
self._attr_available = False
|
||||||
|
return
|
||||||
|
|
||||||
|
self._attr_available = True
|
||||||
|
self._attr_is_on = state.state == STATE_ON
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
@ -137,3 +111,6 @@ class LightSwitch(LightEntity):
|
|||||||
self.hass, [self._switch_entity_id], async_state_changed_listener
|
self.hass, [self._switch_entity_id], async_state_changed_listener
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Call once on adding
|
||||||
|
async_state_changed_listener()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user