forked from asev/homeassistant-uponor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.py
More file actions
143 lines (119 loc) · 5.71 KB
/
Copy pathsensor.py
File metadata and controls
143 lines (119 loc) · 5.71 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
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
import logging
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass, SensorStateClass
from homeassistant.const import UnitOfTemperature, PERCENTAGE
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DOMAIN, SIGNAL_UPONOR_STATE_UPDATE
_LOGGER = logging.getLogger(__name__) # Lägg till detta
async def async_setup_entry(hass, entry, async_add_entities):
state_proxy = hass.data[DOMAIN]["state_proxy"]
entities = []
for thermostat in hass.data[DOMAIN]["thermostats"]:
room_name = state_proxy.get_room_name(thermostat)
_LOGGER.debug(f"Adding sensors for {room_name} (thermostat ID: {thermostat})")
entities.append(UponorRoomCurrentTemperatureSensor(state_proxy, thermostat))
if state_proxy.has_floor_temperature(thermostat):
entities.append(UponorFloorTemperatureSensor(state_proxy, thermostat))
_LOGGER.debug(f"Added floor sensor for: {room_name}")
if state_proxy.has_humidity_sensor(thermostat):
entities.append(UponorHumiditySensor(state_proxy, thermostat))
_LOGGER.debug(f"Added humidity sensor for: {room_name}")
_LOGGER.debug(f"Total number of sensors added: {len(entities)}")
async_add_entities(entities, update_before_add=False)
class UponorFloorTemperatureSensor(SensorEntity):
def __init__(self, state_proxy, thermostat):
self._state_proxy = state_proxy
self._thermostat = thermostat
self._attr_name = f"{state_proxy.get_room_name(thermostat)} Floor Temperature"
self._attr_unique_id = f"{state_proxy.get_thermostat_id(thermostat)}_floor_temp"
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
self._attr_state_class = SensorStateClass.MEASUREMENT
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._state_proxy.get_thermostat_id(self._thermostat))},
"name": self._state_proxy.get_room_name(self._thermostat),
"manufacturer": "Uponor",
"model": self._state_proxy.get_model(),
"sw_version": self._state_proxy.get_version(self._thermostat)
}
@property
def native_value(self):
return self._state_proxy.get_floor_temperature(self._thermostat)
async def async_added_to_hass(self):
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_UPONOR_STATE_UPDATE, self._update_callback
)
)
@callback
def _update_callback(self):
"""Update sensor state. when data updates"""
_LOGGER.debug(f"Updating state for {self._attr_name} with ID {self._attr_unique_id}")
self.async_write_ha_state()
class UponorRoomCurrentTemperatureSensor(SensorEntity):
def __init__(self, state_proxy, thermostat):
self._state_proxy = state_proxy
self._thermostat = thermostat
self._attr_name = f"{state_proxy.get_room_name(thermostat)} Current Temperature"
self._attr_unique_id = f"{state_proxy.get_thermostat_id(thermostat)}_current_temp"
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
self._attr_state_class = SensorStateClass.MEASUREMENT
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._state_proxy.get_thermostat_id(self._thermostat))},
"name": self._state_proxy.get_room_name(self._thermostat),
"manufacturer": "Uponor",
"model": self._state_proxy.get_model(),
"sw_version": self._state_proxy.get_version(self._thermostat)
}
@property
def native_value(self):
return self._state_proxy.get_temperature(self._thermostat)
async def async_added_to_hass(self):
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_UPONOR_STATE_UPDATE, self._update_callback
)
)
@callback
def _update_callback(self):
"""Update sensor state. when data updates"""
_LOGGER.debug(f"Updating state for {self._attr_name} with ID {self._attr_unique_id}")
self.async_write_ha_state()
class UponorHumiditySensor(SensorEntity):
def __init__(self, state_proxy, thermostat):
self._state_proxy = state_proxy
self._thermostat = thermostat
self._attr_name = f"{state_proxy.get_room_name(thermostat)} humidity"
self._attr_unique_id = f"{state_proxy.get_thermostat_id(thermostat)}_rh"
self._attr_device_class = SensorDeviceClass.HUMIDITY
self._attr_native_unit_of_measurement = PERCENTAGE
self._attr_state_class = SensorStateClass.MEASUREMENT
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._state_proxy.get_thermostat_id(self._thermostat))},
"name": self._state_proxy.get_room_name(self._thermostat),
"manufacturer": "Uponor",
"model": self._state_proxy.get_model(),
"sw_version": self._state_proxy.get_version(self._thermostat)
}
@property
def available(self):
"""Return True if the sensor is available."""
return self._state_proxy.has_humidity_sensor(self._thermostat)
@property
def native_value(self):
return self._state_proxy.get_humidity(self._thermostat)
async def async_added_to_hass(self):
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_UPONOR_STATE_UPDATE, self._update_callback
)
)
@callback
def _update_callback(self):
_LOGGER.debug(f"Updating state for {self._attr_name} with ID {self._attr_unique_id}")
self.async_write_ha_state()