-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
151 lines (128 loc) · 4.72 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
151
"""Platform for sensor integration."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
PERCENTAGE,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from . import DysonEntity
from .const import DOMAIN
from .dyson import DysonPureCool
_LOGGER = logging.getLogger(__name__)
SENSOR_TYPES = [
{
"id": "temperature",
"name": "Temperature",
"device_class": SensorDeviceClass.TEMPERATURE,
"native_unit_of_measurement": UnitOfTemperature.CELSIUS,
"state_class": SensorStateClass.MEASUREMENT,
"icon": "mdi:thermometer",
"suggested_display_precision": 1
},
{
"id": "humidity",
"name": "Humidity",
"device_class": SensorDeviceClass.HUMIDITY,
"native_unit_of_measurement": PERCENTAGE,
"state_class": SensorStateClass.MEASUREMENT,
"icon": "mdi:water-percent"
},
{
"id": "pm25",
"name": "Particulate matter (PM2.5)",
"device_class": SensorDeviceClass.PM25,
"native_unit_of_measurement": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
"state_class": SensorStateClass.MEASUREMENT,
"icon": "mdi:blur-radial"
},
{
"id": "pm10",
"name": "Particulate matter (PM10)",
"device_class": SensorDeviceClass.PM10,
"native_unit_of_measurement": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
"state_class": SensorStateClass.MEASUREMENT,
"icon": "mdi:blur-radial"
},
{
"id": "va10",
"name": "Volatile organic compounds",
"device_class": SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
"native_unit_of_measurement": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
"state_class": SensorStateClass.MEASUREMENT,
"icon": "mdi:molecule"
},
{
"id": "noxl",
"name": "Nitrogen dioxide and other oxidising gases",
"device_class": SensorDeviceClass.NITROGEN_DIOXIDE,
"native_unit_of_measurement": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
"state_class": SensorStateClass.MEASUREMENT,
"icon": "mdi:molecule"
},
{
"id": "hflr",
"name": "HEPA filter life",
"native_unit_of_measurement": PERCENTAGE,
"state_class": SensorStateClass.MEASUREMENT,
"entity_category": EntityCategory.DIAGNOSTIC,
"icon": "mdi:alpha-h-box-outline"
},
{
"id": "cflr",
"name": "Carbon filter life",
"native_unit_of_measurement": PERCENTAGE,
"state_class": SensorStateClass.MEASUREMENT,
"entity_category": EntityCategory.DIAGNOSTIC,
"icon": "mdi:alpha-c-box-outline"
}
]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback,) -> None:
"""Set up Dyson Pure Cool sensors from a config entry."""
device = hass.data[DOMAIN][config_entry.entry_id]
coordinator = hass.data[DOMAIN][f"{config_entry.entry_id}_coordinator"]
async_add_entities(
DysonSensorEntity(coordinator, device, data) for data in SENSOR_TYPES
)
class DysonSensorEntity(SensorEntity, DysonEntity):
"""Representation of a dyson sensor entity."""
def __init__(self, coordinator: DataUpdateCoordinator, device: DysonPureCool, description: dict[str, Any]):
"""Initialize a dyson sensor entity."""
super().__init__(coordinator, device, description["name"], description["id"])
for key, value in description.items():
if key in ["name", "id"]:
continue
name = f"_attr_{key}"
setattr(self, name, value)
@property
def native_value(self):
"""Return the value reported by the sensor."""
match self._id:
case "temperature":
return self._device.temperature
case "humidity":
return self._device.humidity
case "pm25":
return self._device.pm25
case "pm10":
return self._device.pm10
case "va10":
return self._device.voc
case "noxl":
return self._device.nox
case "hflr":
return self._device.hepa_filter_life
case "cflr":
return self._device.carbon_filter_life
return None