Skip to content

Commit

Permalink
Fix sensor unit handling, fixes #79 and fixes #80
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptk committed Jan 3, 2024
1 parent 3833c6c commit d39a30e
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions custom_components/blitzortung/sensor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import logging

from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME, LENGTH_KILOMETERS
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME, DEGREE, UnitOfLength
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.helpers.device_registry import DeviceEntryType

from .const import (
ATTR_LAT,
ATTR_LIGHTNING_AZIMUTH,
ATTR_LIGHTNING_COUNTER,
ATTR_LIGHTNING_DISTANCE,
ATTR_LON,
ATTRIBUTION,
DOMAIN,
Expand All @@ -21,8 +20,6 @@
ATTR_UNIT = "unit"
ATTR_LIGHTNING_PROPERTY = "lightning_prop"

DEGREE = "°"

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -74,7 +71,6 @@ def __init__(self, coordinator, integration_name, unique_prefix):
self.entity_id = f"sensor.{integration_name}-{self.name}"
self._unique_id = f"{unique_prefix}-{self.kind}"
self._device_class = None
self._state = None
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}

should_poll = False
Expand All @@ -94,11 +90,6 @@ def name(self):
"""Return the name."""
return f"Lightning {self.label}"

@property
def state(self):
"""Return the state."""
return self._state

@property
def extra_state_attributes(self):
"""Return the state attributes."""
Expand Down Expand Up @@ -142,21 +133,21 @@ class LightningSensor(BlitzortungSensor):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._state = self.INITIAL_STATE
self._attr_native_value = self.INITIAL_STATE

def tick(self):
if self._state != self.INITIAL_STATE and self.coordinator.is_inactive:
self._state = self.INITIAL_STATE
if self._attr_native_value != self.INITIAL_STATE and self.coordinator.is_inactive:
self._attr_native_value = self.INITIAL_STATE
self.async_write_ha_state()


class DistanceSensor(LightningSensor):
kind = ATTR_LIGHTNING_DISTANCE
device_class = ATTR_LIGHTNING_DISTANCE
_attr_native_unit_of_measurement = LENGTH_KILOMETERS
kind = SensorDeviceClass.DISTANCE
device_class = SensorDeviceClass.DISTANCE
_attr_native_unit_of_measurement = UnitOfLength.KILOMETERS

def update_lightning(self, lightning):
self._state = lightning["distance"]
self._attr_native_value = lightning["distance"]
self._attrs[ATTR_LAT] = lightning[ATTR_LAT]
self._attrs[ATTR_LON] = lightning[ATTR_LON]
self.async_write_ha_state()
Expand All @@ -167,7 +158,7 @@ class AzimuthSensor(LightningSensor):
_attr_native_unit_of_measurement = DEGREE

def update_lightning(self, lightning):
self._state = lightning["azimuth"]
self._attr_native_value = lightning["azimuth"]
self._attrs[ATTR_LAT] = lightning[ATTR_LAT]
self._attrs[ATTR_LON] = lightning[ATTR_LON]
self.async_write_ha_state()
Expand All @@ -179,7 +170,7 @@ class CounterSensor(LightningSensor):
INITIAL_STATE = 0

def update_lightning(self, lightning):
self._state = self._state + 1
self._attr_native_value = self._attr_native_value + 1
self.async_write_ha_state()


Expand Down Expand Up @@ -218,9 +209,9 @@ def on_message(self, topic, message):
if topic == self._topic:
payload = message.payload.decode("utf-8")
try:
self._state = self.data_type(payload)
self._attr_native_value = self.data_type(payload)
except ValueError:
self._state = str(payload)
self._attr_native_value = str(payload)
if self.hass:
self.async_write_ha_state()

Expand Down

0 comments on commit d39a30e

Please sign in to comment.