Skip to content

Commit

Permalink
Merge pull request #65 from killer0071234/53-diagnostic-sensor-for-ip…
Browse files Browse the repository at this point in the history
…-port-causes-all-entities-in-invalid

Unit logic was wrong for empty units
  • Loading branch information
killer0071234 authored May 18, 2023
2 parents 7ae1c72 + f7cfced commit da0cdbc
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions custom_components/hiq/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@
from datetime import datetime

from cybro import VarType
from homeassistant.components.sensor import (SensorDeviceClass, SensorEntity,
SensorStateClass)
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (PERCENTAGE, UnitOfElectricCurrent,
UnitOfElectricPotential, UnitOfEnergy,
UnitOfFrequency, UnitOfPower, UnitOfSpeed,
UnitOfTemperature, UnitOfTime)
from homeassistant.const import (
PERCENTAGE,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

from .const import (AREA_ENERGY, AREA_SYSTEM, AREA_WEATHER, ATTR_DESCRIPTION,
CONF_IGNORE_GENERAL_ERROR, DEVICE_DESCRIPTION, DOMAIN,
LOGGER, MANUFACTURER, MANUFACTURER_URL)
from .const import (
AREA_ENERGY,
AREA_SYSTEM,
AREA_WEATHER,
ATTR_DESCRIPTION,
CONF_IGNORE_GENERAL_ERROR,
DEVICE_DESCRIPTION,
DOMAIN,
LOGGER,
MANUFACTURER,
MANUFACTURER_URL,
)
from .coordinator import HiqDataUpdateCoordinator
from .light import is_general_error_ok
from .models import HiqEntity
Expand Down Expand Up @@ -79,7 +98,7 @@ def add_system_tags(
coordinator,
f"{var_prefix}sys.ip_port",
"",
"",
None,
VarType.STR,
EntityCategory.DIAGNOSTIC,
None,
Expand Down Expand Up @@ -435,7 +454,7 @@ def __init__(
coordinator: HiqDataUpdateCoordinator,
var_name: str = "",
var_description: str = "",
var_unit: str = "",
var_unit: str | None = None,
var_type: VarType = VarType.INT,
attr_entity_category: EntityCategory = None,
attr_device_class: SensorDeviceClass = None,
Expand All @@ -447,7 +466,7 @@ def __init__(
super().__init__(coordinator=coordinator)
if var_name == "":
return
self._unit_of_measurement = var_unit
self._attr_native_unit_of_measurement = var_unit
self._unique_id = var_name
self._attr_unique_id = var_name
self._attr_name = var_description if var_description != "" else var_name
Expand Down Expand Up @@ -478,11 +497,6 @@ def device_info(self):
model=DEVICE_DESCRIPTION,
)

@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of the device."""
return self._unit_of_measurement

@property
def native_value(self) -> datetime | StateType | None:
"""Return the state of the sensor."""
Expand Down

0 comments on commit da0cdbc

Please sign in to comment.