-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
executable file
·150 lines (119 loc) · 4.6 KB
/
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
"""iParcelBox basic sensor entity for testing."""
from __future__ import annotations
import asyncio
import httpx
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.const import PERCENTAGE
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,
IPARCELBOX_MESSAGE_SIGNAL,
SENSORS,
BATTERY_LEVEL,
ROUTER_RSSI,
LAST_OPENED
)
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 SENSORS:
_LOGGER.debug("Need to add sensor: %s", sensor)
sensor_object = iParcelBoxStatus(hass, iparcelbox, iparcelbox_info, sensor)
sensors.append(sensor_object)
# _LOGGER.debug("Attempting to add sensor: %s", sensor_object.name)
async_add_entities(sensors)
class iParcelBoxStatus(iParcelBoxEntity, SensorEntity):
"""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._unique_id = f"{self._iparcelbox._mac}-{sensor}"
self._remove_signal_update = None
# _LOGGER.debug("Init sensor: %s", self._unique_id)
if sensor == BATTERY_LEVEL:
self._device_class = SensorDeviceClass.BATTERY
self._state = 'Not installed'
@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 unit_of_measurement(self):
"""Return the unit of measurement."""
if self._sensor == ROUTER_RSSI:
return PERCENTAGE
if self._sensor == BATTERY_LEVEL and self._state != 'Not installed':
return PERCENTAGE
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,
)
self._remove_signal_update = async_dispatcher_connect(
self.hass,
IPARCELBOX_MESSAGE_SIGNAL.format(self._iparcelbox._mac),
self._update_message_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):
if self._sensor in data:
"""Call update method."""
_LOGGER.debug("iParcelBox Sensor callback: %s", data[self._sensor])
self._state = data[self._sensor]
self.async_schedule_update_ha_state(True)
@callback
def _update_message_callback(self, data):
if self._sensor in data:
"""Call update method."""
_LOGGER.debug("iParcelBox Message callback: %s", data[self._sensor])
self._state = data[self._sensor]
self.async_schedule_update_ha_state(True)
# @asyncio.coroutine
# def _async_getStatus(device):
# return device.getStatus()