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:
Franck Nijhof 2021-06-16 08:29:31 +02:00 committed by GitHub
parent ff0c753c87
commit 37d3a4dd53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,6 @@
"""Demo platform that has two fake switches."""
from __future__ import annotations
from homeassistant.components.switch import SwitchEntity
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."""
async_add_entities(
[
DemoSwitch("swith1", "Decorative Lights", True, None, True),
DemoSwitch("switch1", "Decorative Lights", True, None, True),
DemoSwitch(
"swith2",
"switch2",
"AC",
False,
"mdi:air-conditioner",
@ -30,78 +32,42 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class DemoSwitch(SwitchEntity):
"""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."""
self._unique_id = unique_id
self._name = name or DEVICE_DEFAULT_NAME
self._state = state
self._icon = icon
self._assumed = assumed
self._device_class = device_class
self._attr_assumed_state = assumed
self._attr_device_class = device_class
self._attr_icon = icon
self._attr_is_on = state
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_today_energy_kwh = 15
self._attr_unique_id = unique_id
@property
def device_info(self):
"""Return device info."""
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
"identifiers": {(DOMAIN, self.unique_id)},
"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):
"""Turn the switch on."""
self._state = True
self._attr_is_on = True
self._attr_current_power_w = 100
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn the device off."""
self._state = False
self._attr_is_on = False
self._attr_current_power_w = 0
self.schedule_update_ha_state()