2019-04-03 17:40:03 +02:00
|
|
|
"""Demo platform that has two fake switches."""
|
2021-06-16 08:29:31 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2015-06-13 14:56:20 -07:00
|
|
|
from homeassistant.const import DEVICE_DEFAULT_NAME
|
2019-12-08 17:59:27 +01:00
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
from . import DOMAIN
|
2015-02-28 07:31:39 -08:00
|
|
|
|
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the demo switches."""
|
2019-11-13 16:37:31 +01:00
|
|
|
async_add_entities(
|
2019-07-31 12:25:30 -07:00
|
|
|
[
|
2021-06-16 08:29:31 +02:00
|
|
|
DemoSwitch("switch1", "Decorative Lights", True, None, True),
|
2020-03-11 16:16:22 +01:00
|
|
|
DemoSwitch(
|
2021-06-16 08:29:31 +02:00
|
|
|
"switch2",
|
2020-03-11 16:16:22 +01:00
|
|
|
"AC",
|
|
|
|
False,
|
|
|
|
"mdi:air-conditioner",
|
|
|
|
False,
|
|
|
|
device_class="outlet",
|
|
|
|
),
|
2019-07-31 12:25:30 -07:00
|
|
|
]
|
|
|
|
)
|
2015-02-28 07:31:39 -08:00
|
|
|
|
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Demo config entry."""
|
|
|
|
await async_setup_platform(hass, {}, async_add_entities)
|
|
|
|
|
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
class DemoSwitch(SwitchEntity):
|
2016-07-12 16:46:29 +02:00
|
|
|
"""Representation of a demo switch."""
|
2016-03-08 13:35:39 +01:00
|
|
|
|
2021-06-16 08:29:31 +02:00
|
|
|
_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:
|
2016-07-09 00:35:55 -07:00
|
|
|
"""Initialize the Demo switch."""
|
2021-06-16 08:29:31 +02:00
|
|
|
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_unique_id = unique_id
|
2015-02-28 07:31:39 -08:00
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
2021-06-16 08:29:31 +02:00
|
|
|
"identifiers": {(DOMAIN, self.unique_id)},
|
2019-11-13 16:37:31 +01:00
|
|
|
"name": self.name,
|
|
|
|
}
|
|
|
|
|
2015-02-28 07:31:39 -08:00
|
|
|
def turn_on(self, **kwargs):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Turn the switch on."""
|
2021-06-16 08:29:31 +02:00
|
|
|
self._attr_is_on = True
|
2016-11-16 08:26:29 -08:00
|
|
|
self.schedule_update_ha_state()
|
2015-02-28 07:31:39 -08:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Turn the device off."""
|
2021-06-16 08:29:31 +02:00
|
|
|
self._attr_is_on = False
|
2016-11-16 08:26:29 -08:00
|
|
|
self.schedule_update_ha_state()
|