2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Vera switches."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 21:36:48 +00:00
|
|
|
from typing import Any
|
2020-09-16 21:23:50 +00:00
|
|
|
|
|
|
|
import pyvera as veraApi
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2022-01-20 06:46:26 +00:00
|
|
|
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-20 06:46:26 +00:00
|
|
|
from homeassistant.const import Platform
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 21:36:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
from . import VeraDevice
|
2020-09-15 03:06:52 +00:00
|
|
|
from .common import ControllerData, get_controller_data
|
2015-09-09 03:11:25 +00:00
|
|
|
|
2015-03-08 14:58:11 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
2021-05-04 21:36:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-03 07:49:50 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
2020-09-15 03:06:52 +00:00
|
|
|
controller_data = get_controller_data(hass, entry)
|
2020-04-03 07:49:50 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
2020-09-15 03:06:52 +00:00
|
|
|
VeraSwitch(device, controller_data)
|
2022-01-20 06:46:26 +00:00
|
|
|
for device in controller_data.devices[Platform.SWITCH]
|
2021-02-05 12:20:15 +00:00
|
|
|
],
|
|
|
|
True,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2015-03-08 14:58:11 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
class VeraSwitch(VeraDevice[veraApi.VeraSwitch], SwitchEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a Vera Switch."""
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
def __init__(
|
|
|
|
self, vera_device: veraApi.VeraSwitch, controller_data: ControllerData
|
2021-05-20 15:00:19 +00:00
|
|
|
) -> None:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the Vera device."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self._state = False
|
2020-09-15 03:06:52 +00:00
|
|
|
VeraDevice.__init__(self, vera_device, controller_data)
|
2017-03-11 18:06:46 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn device on."""
|
2015-03-02 10:09:00 +00:00
|
|
|
self.vera_device.switch_on()
|
2017-04-04 10:02:09 +00:00
|
|
|
self._state = True
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn device off."""
|
2015-03-02 10:09:00 +00:00
|
|
|
self.vera_device.switch_off()
|
2017-04-04 10:02:09 +00:00
|
|
|
self._state = False
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-03-02 10:09:00 +00:00
|
|
|
|
|
|
|
@property
|
2020-09-16 21:23:50 +00:00
|
|
|
def is_on(self) -> bool:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if device is on."""
|
2016-03-15 09:17:09 +00:00
|
|
|
return self._state
|
2016-01-15 21:30:58 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
def update(self) -> None:
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Update device state."""
|
2021-02-08 14:25:54 +00:00
|
|
|
super().update()
|
2016-03-15 09:17:09 +00:00
|
|
|
self._state = self.vera_device.is_switched_on()
|