Skip to content

Commit 34fcba6

Browse files
committed
added Binary Sensor Thing
1 parent 7ddbd4a commit 34fcba6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/ham/binary_sensor.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import logging
2+
3+
from .things import Thing
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
class BinarySensor(Thing):
9+
"""Basic class for a Binary Sensor entity.
10+
11+
Binary Sensors are things that only publish an ON or OFF state.
12+
13+
This is implemented in this library through a write-only property, called
14+
`state`. Other Sensor subclasses may reimplement them; the responsible of
15+
publishing stuff is the Thing.publish_state method.
16+
"""
17+
device_class: str
18+
enabled_by_default: bool
19+
encoding: str
20+
entity_category: str
21+
expire_after: int
22+
force_update: bool
23+
icon: str
24+
25+
config_fields = [
26+
"device_class", "enabled_by_default", "encoding", "entity_category",
27+
"expire_after", "force_update", "icon"
28+
]
29+
30+
@property
31+
def component(self):
32+
return "binary_sensor"
33+
34+
@property
35+
def state(self):
36+
raise AttributeError("Canonical implementation of binary_sensor has a write-only state")
37+
38+
@state.setter
39+
def state(self, value: bool):
40+
self.publish_state(value)
41+
42+
def get_config(self):
43+
config = super().get_config()
44+
config["state_topic"] = f'~/{ self.short_id }/main'
45+
return config

0 commit comments

Comments
 (0)