-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsensor.py
More file actions
63 lines (47 loc) · 2.15 KB
/
sensor.py
File metadata and controls
63 lines (47 loc) · 2.15 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
import logging
from datetime import date, datetime
from decimal import Decimal
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.typing import StateType
from pyextron import DeviceType, SurroundSoundProcessor
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData
from custom_components.extron.const import CONF_DEVICE_TYPE
logger = logging.getLogger(__name__)
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities):
# Extract stored runtime data from the entry
runtime_data: ExtronConfigEntryRuntimeData = entry.runtime_data
device = runtime_data.device
device_information = runtime_data.device_information
# Add entities
if entry.data[CONF_DEVICE_TYPE] == DeviceType.SURROUND_SOUND_PROCESSOR.value:
ssp = SurroundSoundProcessor(device)
async_add_entities([ExtronDeviceTemperature(ssp, device_information)])
class ExtronDeviceTemperature(SensorEntity):
def __init__(self, ssp: SurroundSoundProcessor, device_information: DeviceInformation) -> None:
self._ssp = ssp
self._device_information = device_information
self._native_value = None
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = "°C"
_attr_state_class = SensorStateClass.MEASUREMENT
@property
def unique_id(self) -> str | None:
return f"extron_{self._device_information.mac_address}_temperature"
@property
def device_info(self) -> DeviceInfo:
return self._device_information.device_info
@property
def name(self):
return f"Extron {self._device_information.model_name} temperature"
@property
def native_value(self) -> StateType | date | datetime | Decimal:
return self._native_value
async def async_update(self):
try:
self._native_value = await self._ssp.get_temperature()
except Exception:
self._attr_available = False
else:
self._attr_available = True