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