core/homeassistant/components/demo/switch.py

72 lines
2.0 KiB
Python
Raw Normal View History

"""Demo platform that has two fake switches."""
from __future__ import annotations
2022-07-04 18:59:52 +00:00
from typing import Any
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
2021-10-22 15:00:00 +00:00
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the demo switch platform."""
async_add_entities(
2019-07-31 19:25:30 +00:00
[
DemoSwitch("switch1", "Decorative Lights", True, None, True),
DemoSwitch(
"switch2",
"AC",
False,
"mdi:air-conditioner",
False,
device_class=SwitchDeviceClass.OUTLET,
),
2019-07-31 19:25:30 +00:00
]
)
class DemoSwitch(SwitchEntity):
"""Representation of a demo switch."""
2016-03-08 12:35:39 +00:00
_attr_has_entity_name = True
_attr_name = None
_attr_should_poll = False
def __init__(
self,
unique_id: str,
device_name: str,
state: bool,
icon: str | None,
assumed: bool,
device_class: SwitchDeviceClass | None = None,
) -> None:
"""Initialize the Demo switch."""
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
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=device_name,
2021-10-22 15:00:00 +00:00
)
2022-07-04 18:59:52 +00:00
def turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
self._attr_is_on = True
self.schedule_update_ha_state()
2022-07-04 18:59:52 +00:00
def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
self._attr_is_on = False
self.schedule_update_ha_state()