mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Define SwitchEntity entity attributes as class variables (#51232)
This commit is contained in:
parent
3d45f00fad
commit
6b0e57e641
@ -1,22 +1,27 @@
|
||||
"""Component to interface with switches that can be controlled remotely."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import final
|
||||
from typing import Any, cast, final
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.config_validation import ( # noqa: F401
|
||||
PLATFORM_SCHEMA,
|
||||
PLATFORM_SCHEMA_BASE,
|
||||
)
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
@ -55,7 +60,7 @@ def is_on(hass, entity_id):
|
||||
return hass.states.is_state(entity_id, STATE_ON)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Track states and offer events for switches."""
|
||||
component = hass.data[DOMAIN] = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
||||
@ -69,37 +74,41 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up a config entry."""
|
||||
return await hass.data[DOMAIN].async_setup_entry(entry)
|
||||
return cast(bool, await hass.data[DOMAIN].async_setup_entry(entry))
|
||||
|
||||
|
||||
async def async_unload_entry(hass, entry):
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
return await hass.data[DOMAIN].async_unload_entry(entry)
|
||||
return cast(bool, await hass.data[DOMAIN].async_unload_entry(entry))
|
||||
|
||||
|
||||
class SwitchEntity(ToggleEntity):
|
||||
"""Base class for switch entities."""
|
||||
|
||||
_attr_current_power_w: float | None = None
|
||||
_attr_is_standby: bool | None = None
|
||||
_attr_today_energy_kwh: float | None = None
|
||||
|
||||
@property
|
||||
def current_power_w(self):
|
||||
def current_power_w(self) -> float | None:
|
||||
"""Return the current power usage in W."""
|
||||
return None
|
||||
return self._attr_current_power_w
|
||||
|
||||
@property
|
||||
def today_energy_kwh(self):
|
||||
def today_energy_kwh(self) -> float | None:
|
||||
"""Return the today total energy usage in kWh."""
|
||||
return None
|
||||
return self._attr_today_energy_kwh
|
||||
|
||||
@property
|
||||
def is_standby(self):
|
||||
def is_standby(self) -> bool | None:
|
||||
"""Return true if device is in standby."""
|
||||
return None
|
||||
return self._attr_is_standby
|
||||
|
||||
@final
|
||||
@property
|
||||
def state_attributes(self):
|
||||
def state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the optional state attributes."""
|
||||
data = {}
|
||||
|
||||
@ -110,11 +119,6 @@ class SwitchEntity(ToggleEntity):
|
||||
|
||||
return data
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
return None
|
||||
|
||||
|
||||
class SwitchDevice(SwitchEntity):
|
||||
"""Representation of a switch (for backwards compatibility)."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user