Skip to content

Commit d39822b

Browse files
authored
Merge pull request #136 from madmats2137/main
fix datetime errors + sensor units
2 parents a314e54 + cb491c1 commit d39822b

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

custom_components/dirigera_platform/base_classes.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
from homeassistant.components.fan import FanEntity, FanEntityFeature
1010
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
1111
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
12+
from homeassistant.const import (
13+
EVENT_HOMEASSISTANT_STOP,
14+
PERCENTAGE,
15+
SIGNAL_STRENGTH_DECIBELS,
16+
EntityCategory,
17+
UnitOfElectricCurrent,
18+
UnitOfElectricPotential,
19+
UnitOfEnergy,
20+
UnitOfPower,
21+
UnitOfTemperature,
22+
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
23+
)
1224

1325
from dirigera import Hub
1426
from dirigera.devices.blinds import Blind
@@ -118,11 +130,11 @@ def schedule_update_ha_state(self, force_refresh:bool = False) -> None:
118130
listener.schedule_update_ha_state(force_refresh)
119131

120132
class ikea_base_device_sensor():
121-
def __init__(self, device, id_suffix:str = "", name:str = "", uom="", icon="", device_class=None, entity_category=None, state_class=None):
133+
def __init__(self, device, id_suffix:str = "", name:str = "", native_unit_of_measurement="", icon="", device_class=None, entity_category=None, state_class=None):
122134
self._device = device
123135
self._name = name
124136
self._id_suffix = id_suffix
125-
self._uom = uom
137+
self._native_unit_of_measurement = native_unit_of_measurement
126138
self._device_class = device_class
127139
self._entity_category = entity_category
128140
self._state_class = state_class
@@ -169,7 +181,7 @@ def icon(self):
169181

170182
@property
171183
def native_unit_of_measurement(self) -> str:
172-
return self._uom
184+
return self._native_unit_of_measurement
173185

174186
async def async_update(self):
175187
await self._device.async_update()
@@ -366,7 +378,7 @@ def __init__(self, device: ikea_vindstyrka_device) -> None:
366378
id_suffix="TEMP",
367379
name="Temperature",
368380
device_class=SensorDeviceClass.TEMPERATURE,
369-
uom="°C",
381+
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
370382
state_class="measurement")
371383
logger.debug("ikea_vindstyrka_temperature ctor...")
372384

@@ -382,7 +394,7 @@ def __init__(self, device: ikea_vindstyrka_device) -> None:
382394
id_suffix="HUM",
383395
name="Humidity",
384396
device_class=SensorDeviceClass.HUMIDITY,
385-
uom="%")
397+
native_unit_of_measurement=PERCENTAGE)
386398

387399
@property
388400
def native_value(self) -> int:
@@ -415,7 +427,7 @@ def __init__(
415427
id_suffix=id_suffix,
416428
name=name_suffix,
417429
device_class=SensorDeviceClass.PM25,
418-
uom="µg/m³")
430+
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER)
419431

420432
@property
421433
def native_value(self) -> int:
@@ -436,7 +448,7 @@ def __init__(self, device: ikea_vindstyrka_device) -> None:
436448
id_suffix="VOC",
437449
name="VOC Index",
438450
device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
439-
uom="µg/m³")
451+
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER)
440452

441453
@property
442454
def native_value(self) -> int:
@@ -643,7 +655,7 @@ def __init__(
643655
prefix: str,
644656
device_class: SensorDeviceClass,
645657
native_value_prop: str,
646-
native_uom: str,
658+
native_unit_of_measurement: str,
647659
icon_name: str,
648660
):
649661
logger.debug("ikea_starkvind_air_purifier_sensor ctor ...")
@@ -652,7 +664,7 @@ def __init__(
652664
id_suffix=prefix,
653665
name=prefix,
654666
device_class=device_class,
655-
uom=native_uom,
667+
native_unit_of_measurement=native_uom,
656668
icon=icon_name)
657669

658670
self._native_value_prop = native_value_prop
@@ -744,7 +756,9 @@ def __init__(self, device):
744756
device = device,
745757
id_suffix="BP01",
746758
name="Battery Percentage",
747-
uom="%",
759+
native_unit_of_measurement=PERCENTAGE,
760+
state_class=SensorStateClass.MEASUREMENT,
761+
#uom="%",
748762
device_class=SensorDeviceClass.BATTERY,
749763
entity_category=EntityCategory.DIAGNOSTIC)
750764

@@ -758,7 +772,9 @@ def __init__(self, device):
758772
device = device,
759773
id_suffix="CA01",
760774
name="Current Amps",
761-
uom="A",
775+
#uom="A",
776+
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
777+
state_class=SensorStateClass.MEASUREMENT,
762778
icon="mdi:current-ac",
763779
device_class=SensorDeviceClass.CURRENT)
764780

@@ -772,7 +788,7 @@ def __init__(self, device):
772788
device = device,
773789
id_suffix="CAP01",
774790
name="Current Active Power",
775-
uom="W",
791+
native_unit_of_measurement=UnitOfPower.WATT,
776792
icon="mdi:lightning-bolt-outline",
777793
device_class=SensorDeviceClass.POWER)
778794

@@ -781,15 +797,15 @@ def native_value(self):
781797
return getattr(self._device, "current_active_power")
782798

783799
class current_voltage_sensor(ikea_base_device_sensor, SensorEntity):
784-
def __init__(self, device):
785-
super().__init__(device,"current_voltage","CV01",SensorDeviceClass.VOLTAGE,"V","Current Voltage","mdi:power-plug")
786800

787801
def __init__(self, device):
788802
super().__init__(
789803
device = device,
790804
id_suffix="CV01",
791805
name="Current Voltage",
792-
uom="V",
806+
#uom="V",
807+
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
808+
state_class=SensorStateClass.MEASUREMENT,
793809
icon="mdi:power-plug",
794810
device_class=SensorDeviceClass.VOLTAGE)
795811

@@ -803,7 +819,7 @@ def __init__(self, device):
803819
device = device,
804820
id_suffix="TEC01",
805821
name="Total Energy Consumed",
806-
uom="kWh",
822+
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
807823
icon="mdi:lightning-bolt-outline",
808824
device_class=SensorDeviceClass.ENERGY,
809825
state_class=SensorStateClass.TOTAL_INCREASING)
@@ -818,7 +834,7 @@ def __init__(self, device):
818834
device = device,
819835
id_suffix="ELAR01",
820836
name="Energy Consumed at Last Reset",
821-
uom="kWh",
837+
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
822838
icon="mdi:lightning-bolt-outline",
823839
device_class=SensorDeviceClass.ENERGY,
824840
state_class=SensorStateClass.TOTAL_INCREASING)
@@ -832,6 +848,7 @@ def __init__(self, device):
832848
super().__init__(
833849
device = device,
834850
id_suffix="TLER01",
851+
device_class=SensorDeviceClass.TIMESTAMP,
835852
name="Time of Last Energy Reset",
836853
icon="mdi:update")
837854

@@ -863,12 +880,14 @@ def __init__(self, device):
863880
super().__init__( device,
864881
id_suffix="TECLU01",
865882
name="Total Energy Consumed Last Updated",
883+
device_class=SensorDeviceClass.TIMESTAMP,
866884
icon="mdi:update")
867885

868886
def __init__(self, device):
869887
super().__init__(
870888
device = device,
871889
id_suffix="TECLU01",
890+
device_class=SensorDeviceClass.TIMESTAMP,
872891
name="Time Energy Consumed Last Updated",
873892
icon="mdi:update")
874893

custom_components/dirigera_platform/hub_event_listener.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import ssl
88
import re
99
from typing import Any
10-
from datetime import datetime, timedelta
10+
import datetime
11+
from dateutil import parser
1112
from dirigera import Hub
1213

1314
from homeassistant.const import ATTR_ENTITY_ID
1415

1516
logger = logging.getLogger("custom_components.dirigera_platform")
1617

17-
DATE_TIME_FORMAT:str = "%Y-%m-%dT%H:%M:%S.%fZ"
18+
DATE_TIME_FORMAT:str = "%Y-%m-%dT%H:%M:%S.%fZ"
1819

1920
process_events_from = {
2021
"motionSensor" : ["isDetected","isOn","batteryPercentage"],
@@ -162,18 +163,18 @@ def parse_scene_update(self, msg):
162163
entity = registry_value.entity
163164

164165
unique_key = f"{entity.registry_entry.device_id}_{trigger_type}"
165-
last_fired = datetime.now()
166+
last_fired = datetime.datetime.now()
166167
if unique_key in controller_trigger_last_time_map:
167168
last_fired = controller_trigger_last_time_map[unique_key]
168169
logger.debug(f"Found date/time in map for controller : {last_fired}")
169170

170-
controller_trigger_last_time_map[unique_key] = datetime.now()
171+
controller_trigger_last_time_map[unique_key] = datetime.datetime.now()
171172

172173
if "lastTriggered" in msg["data"]:
173174
current_triggered_str = msg["data"]["lastTriggered"]
174175
try:
175-
current_triggered = datetime.strptime(current_triggered_str,"%Y-%m-%dT%H:%M:%S.%fZ")
176-
one_second_delta = timedelta(seconds=1)
176+
current_triggered = parser.parse(current_triggered_str)
177+
one_second_delta = datetime.timedelta(seconds=1)
177178
controller_trigger_last_time_map[unique_key] = current_triggered
178179
logger.debug(f"Updated date/time in map for controller with : {current_triggered}")
179180
if last_fired is not None and one_second_delta > current_triggered - last_fired:
@@ -270,7 +271,7 @@ def on_message(self, ws:Any, ws_msg:str):
270271
if key in ["timeOfLastEnergyReset","totalEnergyConsumedLastUpdated"]:
271272
logger.debug(f"Got into date/time so will set the value accordingly...")
272273
try :
273-
value_to_set = datetime.datetime.strptime(attributes[key], DATE_TIME_FORMAT)
274+
value_to_set = parser.parse(attributes[key])
274275
except:
275276
#Ignore the exception
276277
logger.warning(f"Failed to convert {attributes[key]} to date/time...")

0 commit comments

Comments
 (0)