-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_sensor.py
executable file
·159 lines (125 loc) · 4.74 KB
/
binary_sensor.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""iParcelBox basic sensor entity for testing."""
from __future__ import annotations
import asyncio
import httpx
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_DOOR,
DEVICE_CLASS_LOCK,
DEVICE_CLASS_BATTERY_CHARGING
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.const import PERCENTAGE, DEVICE_CLASS_BATTERY, DEVICE_CLASS_TIMESTAMP
from homeassistant.helpers.httpx_client import get_async_client
# from homeassistant.util.async import (
# run_callback_threadsafe, run_coroutine_threadsafe)
import logging
from .const import (
DOMAIN,
IPARCELBOX,
IPARCELBOX_INFO,
IPARCELBOX_UPDATE_SIGNAL,
BINARY_SENSORS,
LOCK_STATUS,
LID_STATUS,
ASLEEP,
BATTERY_CHARGING,
)
from .entity import iParcelBoxEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the iParcelBox sensor platform."""
sensors = []
config_entry_id = config_entry.entry_id
data = hass.data[DOMAIN][config_entry_id]
iparcelbox = data[IPARCELBOX]
iparcelbox_info = data[IPARCELBOX_INFO]
for sensor in BINARY_SENSORS:
_LOGGER.debug("Need to add binory sensor: %s", sensor)
sensor_object = iParcelBoxBinarySensor(hass, iparcelbox, iparcelbox_info, sensor)
sensors.append(sensor_object)
# _LOGGER.debug("Attempting to add sensor: %s", sensor_object.name)
async_add_entities(sensors)
class iParcelBoxBinarySensor(iParcelBoxEntity, BinarySensorEntity):
"""Representation of a Sensor."""
def __init__(self, hass, iparcelbox, iparcelbox_info, sensor):
"""Initialize the sensor."""
super().__init__(iparcelbox, iparcelbox_info)
self.hass = hass
self._sensor = sensor
self._iparcelbox = iparcelbox
self._state = None
self._device_class = None
self._is_on = None
self._unique_id = f"{self._iparcelbox._mac}-{sensor}"
self._remove_signal_update = None
# _LOGGER.debug("Init sensor: %s", self._unique_id)
if sensor == BATTERY_CHARGING:
self._device_class = DEVICE_CLASS_BATTERY_CHARGING
self._state = 'Not installed'
if sensor == LID_STATUS:
self._device_class = DEVICE_CLASS_DOOR
if sensor == LOCK_STATUS:
self._device_class = DEVICE_CLASS_LOCK
if sensor == ASLEEP:
self._state = 'N/A'
@property
def unique_id(self):
"""Sensor unique id."""
return self._unique_id
@property
def name(self):
"""Return the name of the sensor."""
return f"{self._iparcelbox._name} {self._sensor}"
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._device_class
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def is_on(self):
"""Return the state of the sensor."""
if self._sensor == BATTERY_CHARGING:
return True if self._state == "On" else False
if self._sensor == LID_STATUS:
return True if self._state == "closed" else False
if self._sensor == LOCK_STATUS:
return True if self._state == "unlocked" else False
if self._sensor == ASLEEP:
return self._state
return False
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
iParcelBox: don't do anything here - all updates via _update_callback
"""
# _LOGGER.debug("Calling getStatus")
# self._state = asyncio.run_coroutine_threadsafe(
# self.async_update(), self.hass.loop
# ).result()
# self._state = "Locked"
async def async_added_to_hass(self):
"""Call when entity is added to hass."""
self._remove_signal_update = async_dispatcher_connect(
self.hass,
IPARCELBOX_UPDATE_SIGNAL.format(self._iparcelbox._mac),
self._update_callback,
)
async def async_will_remove_from_hass(self) -> None:
"""Call when entity will be removed from hass."""
self._remove_signal_update()
@callback
def _update_callback(self, data):
"""Call update method."""
if self._sensor in data:
_LOGGER.debug("Binary Sensor: %s - %s", self._sensor, data[self._sensor])
self._state = data[self._sensor]
self.async_schedule_update_ha_state(True)
# @asyncio.coroutine
# def _async_getStatus(device):
# return device.getStatus()