2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Eufy switches."""
|
2022-01-03 15:31:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-08-20 05:52:55 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2019-10-10 18:16:30 +00:00
|
|
|
import lakeside
|
2018-04-11 01:38:23 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-04-11 01:38:23 +00:00
|
|
|
|
|
|
|
|
2022-01-03 15:31:24 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-04-11 01:38:23 +00:00
|
|
|
"""Set up Eufy switches."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([EufySwitch(discovery_info)], True)
|
2018-04-11 01:38:23 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class EufySwitch(SwitchEntity):
|
2018-04-11 01:38:23 +00:00
|
|
|
"""Representation of a Eufy switch."""
|
|
|
|
|
|
|
|
def __init__(self, device):
|
|
|
|
"""Initialize the light."""
|
|
|
|
|
|
|
|
self._state = None
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = device["name"]
|
|
|
|
self._address = device["address"]
|
|
|
|
self._code = device["code"]
|
|
|
|
self._type = device["type"]
|
2018-04-11 01:38:23 +00:00
|
|
|
self._switch = lakeside.switch(self._address, self._code, self._type)
|
|
|
|
self._switch.connect()
|
|
|
|
|
2022-08-20 05:52:55 +00:00
|
|
|
def update(self) -> None:
|
2018-04-11 01:38:23 +00:00
|
|
|
"""Synchronise state from the switch."""
|
|
|
|
self._switch.update()
|
|
|
|
self._state = self._switch.power
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the ID of this light."""
|
|
|
|
return self._address
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device if any."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state
|
|
|
|
|
2022-08-20 05:52:55 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2018-04-11 01:38:23 +00:00
|
|
|
"""Turn the specified switch on."""
|
|
|
|
try:
|
|
|
|
self._switch.set_state(True)
|
|
|
|
except BrokenPipeError:
|
|
|
|
self._switch.connect()
|
|
|
|
self._switch.set_state(power=True)
|
|
|
|
|
2022-08-20 05:52:55 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2018-04-11 01:38:23 +00:00
|
|
|
"""Turn the specified switch off."""
|
|
|
|
try:
|
|
|
|
self._switch.set_state(False)
|
|
|
|
except BrokenPipeError:
|
|
|
|
self._switch.connect()
|
|
|
|
self._switch.set_state(False)
|