Use entity class vars in Switch demo (#51906)
* Use entity class vars in Switch demo * Fix missing initial state of the demo switch
This commit is contained in:
parent
ff0c753c87
commit
37d3a4dd53
1 changed files with 27 additions and 61 deletions
|
@ -1,4 +1,6 @@
|
||||||
"""Demo platform that has two fake switches."""
|
"""Demo platform that has two fake switches."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.const import DEVICE_DEFAULT_NAME
|
from homeassistant.const import DEVICE_DEFAULT_NAME
|
||||||
|
|
||||||
|
@ -9,9 +11,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||||
"""Set up the demo switches."""
|
"""Set up the demo switches."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
DemoSwitch("swith1", "Decorative Lights", True, None, True),
|
DemoSwitch("switch1", "Decorative Lights", True, None, True),
|
||||||
DemoSwitch(
|
DemoSwitch(
|
||||||
"swith2",
|
"switch2",
|
||||||
"AC",
|
"AC",
|
||||||
False,
|
False,
|
||||||
"mdi:air-conditioner",
|
"mdi:air-conditioner",
|
||||||
|
@ -30,78 +32,42 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
class DemoSwitch(SwitchEntity):
|
class DemoSwitch(SwitchEntity):
|
||||||
"""Representation of a demo switch."""
|
"""Representation of a demo switch."""
|
||||||
|
|
||||||
def __init__(self, unique_id, name, state, icon, assumed, device_class=None):
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
unique_id: str,
|
||||||
|
name: str,
|
||||||
|
state: bool,
|
||||||
|
icon: str | None,
|
||||||
|
assumed: bool,
|
||||||
|
device_class: str | None = None,
|
||||||
|
) -> None:
|
||||||
"""Initialize the Demo switch."""
|
"""Initialize the Demo switch."""
|
||||||
self._unique_id = unique_id
|
self._attr_assumed_state = assumed
|
||||||
self._name = name or DEVICE_DEFAULT_NAME
|
self._attr_device_class = device_class
|
||||||
self._state = state
|
self._attr_icon = icon
|
||||||
self._icon = icon
|
self._attr_is_on = state
|
||||||
self._assumed = assumed
|
self._attr_name = name or DEVICE_DEFAULT_NAME
|
||||||
self._device_class = device_class
|
self._attr_today_energy_kwh = 15
|
||||||
|
self._attr_unique_id = unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return device info."""
|
"""Return device info."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {
|
"identifiers": {(DOMAIN, self.unique_id)},
|
||||||
# Serial numbers are unique identifiers within a specific domain
|
|
||||||
(DOMAIN, self.unique_id)
|
|
||||||
},
|
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique id."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No polling needed for a demo switch."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the device if any."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon to use for device if any."""
|
|
||||||
return self._icon
|
|
||||||
|
|
||||||
@property
|
|
||||||
def assumed_state(self):
|
|
||||||
"""Return if the state is based on assumptions."""
|
|
||||||
return self._assumed
|
|
||||||
|
|
||||||
@property
|
|
||||||
def current_power_w(self):
|
|
||||||
"""Return the current power usage in W."""
|
|
||||||
if self._state:
|
|
||||||
return 100
|
|
||||||
|
|
||||||
@property
|
|
||||||
def today_energy_kwh(self):
|
|
||||||
"""Return the today total energy usage in kWh."""
|
|
||||||
return 15
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Return true if switch is on."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return device of entity."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
|
self._attr_current_power_w = 100
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
self._attr_current_power_w = 0
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue