forked from ochorocho/ina_sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
130 lines (103 loc) · 4.26 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
"""Platform for sensor integration."""
from homeassistant.const import POWER_WATT, ELECTRIC_POTENTIAL_VOLT, ELECTRIC_CURRENT_MILLIAMPERE
from homeassistant.helpers.entity import Entity
from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219
from adafruit_extended_bus import ExtendedI2C as I2C
DEFAULT_ICON = "mdi:current-dc"
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the sensor platform."""
address = config.get('address')
device = config.get('device_id')
device_id = device if device else 1
add_entities([
InaCurrent('current', ELECTRIC_CURRENT_MILLIAMPERE, device_id, address),
InaCurrent('bus_voltage', ELECTRIC_POTENTIAL_VOLT, device_id, address),
InaCurrent('shunt_voltage', ELECTRIC_POTENTIAL_VOLT, device_id, address),
InaCurrent('power', POWER_WATT, device_id, address),
InaSupply('supply_voltage', ELECTRIC_POTENTIAL_VOLT, device_id, address)
])
class InaCurrent(Entity):
"""Representation of a Sensor."""
def __init__(self, measure, unit, device, address):
"""Initialize the sensor."""
self._measure = measure
self._unit = unit
i2c_bus = I2C(device)
self._device = device
self._address = address
self.ina219 = INA219(i2c_bus, address)
value = getattr(self.ina219, self._measure)
self._state = round(value, 2)
if measure == 'bus_voltage':
InaData.setLastBusVoltage(round(value,2))
elif measure == 'shunt_voltage':
InaData.setLastShuntVoltage(round(value,2))
@property
def unique_id(self):
"""Return the unique ID for this sensor."""
return 'ina219_dev' + str(self._device) + '_' + hex(self._address) + '_' + self._measure
@property
def name(self):
"""Return the name of the sensor."""
return self._measure.replace('_', ' ').title() + ' ( Dev ' + str(self._device) + ' / INA ' + hex(self._address) + ')'
@property
def icon(self):
"""Return the default icon."""
return DEFAULT_ICON
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit
def update(self):
"""Fetch new state data for the sensor."""
value = getattr(self.ina219, self._measure)
self._state = round(value, 2)
if self._measure == 'bus_voltage':
InaData.setLastBusVoltage(round(value,2))
elif self._measure == 'shunt_voltage':
InaData.setLastShuntVoltage(round(value,2))
class InaSupply(Entity):
"""Representation of a Sensor."""
def __init__(self, measure, unit, device, address):
"""Initialize the sensor."""
self._measure = measure
self._unit = unit
self._device = device
self._address = address
self._state = InaData.getLastBusVoltage() + InaData.getLastShuntVoltage()
@property
def unique_id(self):
"""Return the unique ID for this sensor."""
return 'ina219_dev' + str(self._device) + '_' + hex(self._address) + '_' + self._measure
@property
def name(self):
"""Return the name of the sensor."""
return self._measure.replace('_', ' ').title() + ' ( Dev ' + str(self._device) + ' / INA ' + hex(self._address) + ')'
@property
def icon(self):
"""Return the default icon."""
return DEFAULT_ICON
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit
def update(self):
"""Fetch new state data for the sensor."""
self._state = InaData.getLastBusVoltage() + InaData.getLastShuntVoltage()
class InaData:
def setLastBusVoltage(currentBusVoltage):
InaData.lastBusVoltage = currentBusVoltage
def setLastShuntVoltage(currentShuntVoltage):
InaData.lastShuntVoltage = currentShuntVoltage
def getLastBusVoltage():
return InaData.lastBusVoltage
def getLastShuntVoltage():
return InaData.lastShuntVoltage