-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPowersensorSensorEntity.py
More file actions
94 lines (83 loc) · 3.38 KB
/
PowersensorSensorEntity.py
File metadata and controls
94 lines (83 loc) · 3.38 KB
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
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import EntityCategory, UnitOfPower, UnitOfEnergy, PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from .PowersensorEntity import PowersensorEntity
from .SensorMeasurements import SensorMeasurements
from .const import DOMAIN, SENSOR_NAME_FORMAT
import logging
_LOGGER = logging.getLogger(__name__)
_config = {
# TODO: change names to translation keys
SensorMeasurements.Battery: {
"name": "Battery Level",
"device_class": SensorDeviceClass.BATTERY,
"unit": PERCENTAGE,
"precision": 0,
'event': 'battery_level',
'message_key': 'volts',
'callback': lambda v: max(min(100.0*(v-3.3)/0.85,100),0) # 0% = 3.3 V , 100% = 4.15 V
},
SensorMeasurements.WATTS: {
"name": "Power",
"device_class": SensorDeviceClass.POWER,
"unit": UnitOfPower.WATT,
"precision": 1,
'event': 'average_power',
'message_key': 'watts',
},
SensorMeasurements.SUMMATION_ENERGY: {
"name": "Total Energy",
"device_class": SensorDeviceClass.ENERGY,
"unit": UnitOfEnergy.KILO_WATT_HOUR,
"precision": 2,
"state_class": SensorStateClass.TOTAL,
'event': 'summation_energy',
'message_key': 'summation_joules',
'callback': lambda v: v / 3600000.0
},
SensorMeasurements.ROLE: {
'name': 'Device Role',
'category': EntityCategory.DIAGNOSTIC,
'event': 'role',
'message_key': 'role',
},
}
class PowersensorSensorEntity(PowersensorEntity):
"""Powersensor Plug Class--designed to handle all measurements of the plug--perhaps less expressive"""
def __init__(self, hass: HomeAssistant, mac : str,
measurement_type: SensorMeasurements):
"""Initialize the sensor."""
super().__init__(hass, mac, _config, measurement_type)
self._model = f"PowersensorSensor"
self.measurement_type = measurement_type
config = _config[measurement_type]
self._measurement_name = config['name']
self._device_name = self._default_device_name()
self._attr_name = f"{self._device_name} {self._measurement_name}"
self._rename_based_on_role()
@property
def device_info(self) -> DeviceInfo:
return {
'identifiers': {(DOMAIN, self._mac)},
'manufacturer': "Powersensor",
'model': self._model,
'name': self._device_name ,
}
def _ensure_matching_prefix(self):
if not self._attr_name.startswith(self._device_name):
self._attr_name = f"{self._device_name} {self._measurement_name }"
def _rename_based_on_role(self):
if self._device_name == self._default_device_name():
if self.role =='house-net' or self.role == "water" or self.role == "solar":
role2name = {
"house-net": "Powersensor Mains Sensor ⚡",
"solar": "Powersensor Solar Sensor ⚡",
"water": "Powersensor Water Sensor 💧",
}
self._device_name = role2name[self.role]
self._ensure_matching_prefix()
return True
return False
def _default_device_name(self):
return SENSOR_NAME_FORMAT % self._mac