Skip to content

Commit

Permalink
Switched to native value/unit for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherjulien committed Sep 3, 2021
1 parent 71be4ca commit 32bfbb2
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions custom_components/myhome/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PLATFORM_SCHEMA,
DOMAIN as PLATFORM,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import (
Expand Down Expand Up @@ -224,10 +225,10 @@ def __init__(self, hass, name: str, who: str, where: str, device_class: str, man
self._attr_unique_id = f"{self._device_id}-{self._type_id}"
self._attr_entity_registry_enabled_default = True
self._attr_device_class = DEVICE_CLASS_POWER
self._attr_unit_of_measurement = POWER_WATT
self._attr_native_unit_of_measurement = POWER_WATT
self._attr_state_class = STATE_CLASS_MEASUREMENT
self._attr_should_poll = False
self._attr_state = 0
self._attr_native_value = 0

async def async_added_to_hass(self):
"""When entity is added to hass."""
Expand All @@ -249,7 +250,7 @@ def handle_event(self, message: OWNEnergyEvent):
"""Handle an event message."""
if message.message_type == MESSAGE_TYPE_ACTIVE_POWER:
LOGGER.info(message.human_readable_log)
self._attr_state = message.active_power
self._attr_native_value = message.active_power
self.async_schedule_update_ha_state()

async def start_sending_instant_power(self, duration):
Expand All @@ -273,17 +274,17 @@ def __init__(self, hass, name: str, who: str, where: str, period: str, manufactu
self._type_id = "daily-energy"
self._type_name = "Energy (today)"
self._attr_entity_registry_enabled_default = False
self._attr_last_reset = dt_util.start_of_local_day()
# self._attr_last_reset = dt_util.start_of_local_day()
elif period == "monthly":
self._type_id = "monthly-energy"
self._type_name = "Energy (current month)"
self._attr_entity_registry_enabled_default = False
self._attr_last_reset = dt_util.start_of_local_day().replace(day=1)
# self._attr_last_reset = dt_util.start_of_local_day().replace(day=1)
else:
self._type_id = "total-energy"
self._type_name = "Energy"
self._attr_entity_registry_enabled_default = True
self._attr_last_reset = dt_util.utc_from_timestamp(0)
# self._attr_last_reset = dt_util.utc_from_timestamp(0)

self._attr_device_info = {
"identifiers": {
Expand All @@ -298,10 +299,10 @@ def __init__(self, hass, name: str, who: str, where: str, period: str, manufactu
self._attr_name = f"{self._device_name} {self._type_name}"
self._attr_unique_id = f"{self._device_id}-{self._type_id}"
self._attr_device_class = DEVICE_CLASS_ENERGY
self._attr_unit_of_measurement = ENERGY_WATT_HOUR
self._attr_state_class = STATE_CLASS_MEASUREMENT
self._attr_native_unit_of_measurement = ENERGY_WATT_HOUR
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
self._attr_should_poll = True
self._attr_state = None
self._attr_native_value = None

async def async_added_to_hass(self):
"""When entity is added to hass."""
Expand All @@ -328,15 +329,15 @@ def handle_event(self, message: OWNEnergyEvent):
"""Handle an event message."""
if self._type_id == "total-energy" and message.message_type == MESSAGE_TYPE_ENERGY_TOTALIZER:
LOGGER.info(message.human_readable_log)
self._attr_state = message.total_consumption
self._attr_native_value = message.total_consumption
elif self._type_id == "monthly-energy" and message.message_type == MESSAGE_TYPE_CURRENT_MONTH_CONSUMPTION:
LOGGER.info(message.human_readable_log)
self._attr_state = message.current_month_partial_consumption
self._attr_last_reset = dt_util.start_of_local_day().replace(day=1)
self._attr_native_value = message.current_month_partial_consumption
# self._attr_last_reset = dt_util.start_of_local_day().replace(day=1)
elif self._type_id == "daily-energy" and message.message_type == MESSAGE_TYPE_CURRENT_DAY_CONSUMPTION:
LOGGER.info(message.human_readable_log)
self._attr_state = message.current_day_partial_consumption
self._attr_last_reset = dt_util.start_of_local_day()
self._attr_native_value = message.current_day_partial_consumption
# self._attr_last_reset = dt_util.start_of_local_day()
self.async_schedule_update_ha_state()

class MyHOMETemperatureSensor(SensorEntity):
Expand Down Expand Up @@ -364,10 +365,10 @@ def __init__(self, hass, name: str, who: str, where: str, device_class: str, man

self._attr_entity_registry_enabled_default = True
self._attr_device_class = DEVICE_CLASS_TEMPERATURE
self._attr_unit_of_measurement = TEMP_CELSIUS
self._attr_native_unit_of_measurement = TEMP_CELSIUS
self._attr_state_class = STATE_CLASS_MEASUREMENT
self._attr_should_poll = True
self._attr_state = None
self._attr_native_value = None

async def async_added_to_hass(self):
"""When entity is added to hass."""
Expand All @@ -389,9 +390,9 @@ def handle_event(self, message: OWNHeatingEvent):
"""Handle an event message."""
if message.message_type == MESSAGE_TYPE_MAIN_TEMPERATURE:
LOGGER.info(message.human_readable_log)
self._attr_state = message.main_temperature
self._attr_native_value = message.main_temperature
self.async_schedule_update_ha_state()
elif message.message_type == MESSAGE_TYPE_SECONDARY_TEMPERATURE:
LOGGER.info(message.human_readable_log)
self._attr_state = message.secondary_temperature[1]
self._attr_native_value = message.secondary_temperature[1]
self.async_schedule_update_ha_state()

0 comments on commit 32bfbb2

Please sign in to comment.