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
|
|
|
|
|
2022-07-04 20:59:52 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2021-12-01 00:38:45 +01:00
|
|
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
2022-01-03 16:24:09 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 04:04:26 +02:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-01-03 16:24:09 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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
|
|
|
|
|
|
|
|
2023-06-14 21:03:07 +02:00
|
|
|
async def async_setup_entry(
|
2022-01-03 16:24:09 +01:00
|
|
|
hass: HomeAssistant,
|
2023-06-14 21:03:07 +02:00
|
|
|
config_entry: ConfigEntry,
|
2022-01-03 16:24:09 +01:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2023-06-14 21:03:07 +02:00
|
|
|
"""Set up the demo switch platform."""
|
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,
|
2021-12-01 00:38:45 +01:00
|
|
|
device_class=SwitchDeviceClass.OUTLET,
|
2020-03-11 16:16:22 +01:00
|
|
|
),
|
2019-07-31 12:25:30 -07:00
|
|
|
]
|
|
|
|
)
|
2015-02-28 07:31:39 -08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-06-14 21:03:07 +02:00
|
|
|
_attr_has_entity_name = True
|
2023-06-19 15:56:48 +02:00
|
|
|
_attr_name = None
|
2021-06-16 08:29:31 +02:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
unique_id: str,
|
2023-06-14 21:03:07 +02:00
|
|
|
device_name: str,
|
2021-06-16 08:29:31 +02:00
|
|
|
state: bool,
|
|
|
|
icon: str | None,
|
|
|
|
assumed: bool,
|
2021-12-01 00:38:45 +01:00
|
|
|
device_class: SwitchDeviceClass | None = None,
|
2021-06-16 08:29:31 +02:00
|
|
|
) -> 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_unique_id = unique_id
|
2022-02-10 10:05:58 +01:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, unique_id)},
|
2023-06-14 21:03:07 +02:00
|
|
|
name=device_name,
|
2021-10-22 11:00:00 -04:00
|
|
|
)
|
2019-11-13 16:37:31 +01:00
|
|
|
|
2022-07-04 20:59:52 +02:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
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
|
|
|
|
2022-07-04 20:59:52 +02:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
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()
|