mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Simplify switch light (#47317)
This commit is contained in:
parent
504e5b77ca
commit
e6a6b2a680
@ -12,7 +12,7 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CALLBACK_TYPE, callback
|
from homeassistant.core import State, callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
@ -37,7 +37,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
hass: HomeAssistantType,
|
hass: HomeAssistantType,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
async_add_entities: Callable[[Sequence[Entity], bool], None],
|
async_add_entities: Callable[[Sequence[Entity]], None],
|
||||||
discovery_info: Optional[DiscoveryInfoType] = None,
|
discovery_info: Optional[DiscoveryInfoType] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Light Switch platform."""
|
"""Initialize Light Switch platform."""
|
||||||
@ -53,8 +53,7 @@ async def async_setup_platform(
|
|||||||
config[CONF_ENTITY_ID],
|
config[CONF_ENTITY_ID],
|
||||||
unique_id,
|
unique_id,
|
||||||
)
|
)
|
||||||
],
|
]
|
||||||
True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -66,9 +65,7 @@ class LightSwitch(LightEntity):
|
|||||||
self._name = name
|
self._name = name
|
||||||
self._switch_entity_id = switch_entity_id
|
self._switch_entity_id = switch_entity_id
|
||||||
self._unique_id = unique_id
|
self._unique_id = unique_id
|
||||||
self._is_on = False
|
self._switch_state: Optional[State] = None
|
||||||
self._available = False
|
|
||||||
self._async_unsub_state_changed: Optional[CALLBACK_TYPE] = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
@ -78,12 +75,16 @@ class LightSwitch(LightEntity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if light switch is on."""
|
"""Return true if light switch is on."""
|
||||||
return self._is_on
|
assert self._switch_state is not None
|
||||||
|
return self._switch_state.state == STATE_ON
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return true if light switch is on."""
|
"""Return true if light switch is on."""
|
||||||
return self._available
|
return (
|
||||||
|
self._switch_state is not None
|
||||||
|
and self._switch_state.state != STATE_UNAVAILABLE
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self) -> bool:
|
def should_poll(self) -> bool:
|
||||||
@ -117,33 +118,20 @@ class LightSwitch(LightEntity):
|
|||||||
context=self._context,
|
context=self._context,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Query the switch in this light switch and determine the state."""
|
|
||||||
switch_state = self.hass.states.get(self._switch_entity_id)
|
|
||||||
|
|
||||||
if switch_state is None:
|
|
||||||
self._available = False
|
|
||||||
return
|
|
||||||
|
|
||||||
self._is_on = switch_state.state == STATE_ON
|
|
||||||
self._available = switch_state.state != STATE_UNAVAILABLE
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
assert self.hass is not None
|
||||||
|
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(*_: Any) -> None:
|
||||||
"""Handle child updates."""
|
"""Handle child updates."""
|
||||||
self.async_schedule_update_ha_state(True)
|
assert self.hass is not None
|
||||||
|
self._switch_state = self.hass.states.get(self._switch_entity_id)
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
assert self.hass is not None
|
self.async_on_remove(
|
||||||
self._async_unsub_state_changed = async_track_state_change_event(
|
async_track_state_change_event(
|
||||||
self.hass, [self._switch_entity_id], async_state_changed_listener
|
self.hass, [self._switch_entity_id], async_state_changed_listener
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
|
||||||
"""Handle removal from Home Assistant."""
|
|
||||||
if self._async_unsub_state_changed is not None:
|
|
||||||
self._async_unsub_state_changed()
|
|
||||||
self._async_unsub_state_changed = None
|
|
||||||
self._available = False
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user