forked from redmcg/fbee_ha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
87 lines (70 loc) · 2.16 KB
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Platform for sensor integration."""
from __future__ import annotations
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import DOMAIN
from .fbee import STATE_NEW_DEV, STATE_NEW_STATE, FBee, NotConnected
def callback(add_entities, d, s):
if not hasattr(d, 'ha'):
d.ha = FBeeSwitch(d)
add_entities([d.ha])
elif s & STATE_NEW_STATE:
d.ha.schedule_update_ha_state()
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
d = FBee(
config["host"],
config["port"],
config["serialnumber"],
[lambda d, s: callback(add_entities, d, s)],
)
d.connect()
if "pollinterval" in config:
i = config["pollinterval"]
else:
i = 60
d.start_async_read(i)
"""Set up the switch platform."""
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
d = hass.data[DOMAIN][entry.entry_id]
d.add_callback([lambda d, s: callback(async_add_entities, d, s)])
class FBeeSwitch(SwitchEntity):
"""Representation of a Switch."""
def __init__(self, d):
"""Initialize the switch."""
self.d = d
@property
def name(self) -> str:
"""Return the name of the switch."""
return self.d.get_name()
@property
def is_on(self) -> bool:
"""Return the state of the switch."""
return self.d.get_state()
@property
def should_poll(self) -> bool:
"""Return if we should poll."""
return False
@property
def unique_id(self) -> str:
return self.d.get_key()
def turn_on(self, **kwargs) -> None:
try:
self.d.push_state(1)
except NotConnected:
pass
def turn_off(self, **kwargs) -> None:
try:
self.d.push_state(0)
except NotConnected:
pass