Define SwitchEntity entity attributes as class variables (#51232)

This commit is contained in:
Franck Nijhof 2021-06-01 10:23:10 +02:00 committed by GitHub
parent 3d45f00fad
commit 6b0e57e641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,22 +1,27 @@
"""Component to interface with switches that can be controlled remotely.""" """Component to interface with switches that can be controlled remotely."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import final from typing import Any, cast, final
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
SERVICE_TOGGLE, SERVICE_TOGGLE,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_ON, STATE_ON,
) )
from homeassistant.core import HomeAssistant
from homeassistant.helpers.config_validation import ( # noqa: F401 from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE, PLATFORM_SCHEMA_BASE,
) )
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
# mypy: allow-untyped-defs, no-check-untyped-defs # 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) 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.""" """Track states and offer events for switches."""
component = hass.data[DOMAIN] = EntityComponent( component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL _LOGGER, DOMAIN, hass, SCAN_INTERVAL
@ -69,37 +74,41 @@ async def async_setup(hass, config):
return True return True
async def async_setup_entry(hass, entry): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry.""" """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.""" """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): class SwitchEntity(ToggleEntity):
"""Base class for switch entities.""" """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 @property
def current_power_w(self): def current_power_w(self) -> float | None:
"""Return the current power usage in W.""" """Return the current power usage in W."""
return None return self._attr_current_power_w
@property @property
def today_energy_kwh(self): def today_energy_kwh(self) -> float | None:
"""Return the today total energy usage in kWh.""" """Return the today total energy usage in kWh."""
return None return self._attr_today_energy_kwh
@property @property
def is_standby(self): def is_standby(self) -> bool | None:
"""Return true if device is in standby.""" """Return true if device is in standby."""
return None return self._attr_is_standby
@final @final
@property @property
def state_attributes(self): def state_attributes(self) -> dict[str, Any] | None:
"""Return the optional state attributes.""" """Return the optional state attributes."""
data = {} data = {}
@ -110,11 +119,6 @@ class SwitchEntity(ToggleEntity):
return data return data
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return None
class SwitchDevice(SwitchEntity): class SwitchDevice(SwitchEntity):
"""Representation of a switch (for backwards compatibility).""" """Representation of a switch (for backwards compatibility)."""