Skip to content

Commit

Permalink
Update use of unit/icon for sensors
Browse files Browse the repository at this point in the history
Try to solve issue #130
  • Loading branch information
pcourbin committed Jan 4, 2025
1 parent 652fb89 commit 756cbfe
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 114 deletions.
20 changes: 20 additions & 0 deletions config/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ ecodevices_rt2:
price_unit_of_measurement: "USD"
price_icon: mdi:account-cash

- name: Counter 2
type: "counter"
id: 1
price_unit_of_measurement:
index_unit_of_measurement: L
price_icon: mdi:currency-eur
index_icon: mdi:water
device_class: "water"

#### DigitalInput Example
- name: DigitalInput 1
type: "digitalinput"
Expand Down Expand Up @@ -75,6 +84,13 @@ ecodevices_rt2:
type: "post"
id: 1

- name: Post 2
type: "post"
id: 2
index_unit_of_measurement: "L"
instant_unit_of_measurement: "L/min"
device_class: "water"

- name: Subpost 2 of Post 1
type: "post"
id: 1
Expand Down Expand Up @@ -135,3 +151,7 @@ ecodevices_rt2:
illuminance_icon: mdi:sun-wireless-outline
temperature_icon: mdi:coolant-temperature
humidity_icon: mdi:cloud-percent

- name: XHTL 2
type: "xthl"
id: 2
3 changes: 3 additions & 0 deletions custom_components/ecodevices_rt2/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
COORDINATOR = "coordinator"
CONFIG = "config"

CONF_STATE_CLASS = "state_class"
CONF_DEVICE_CLASS = "device_class"

UNDO_UPDATE_LISTENER = "undo_update_listener"
DEFAULT_SCAN_INTERVAL = 15
DEFAULT_UPDATE_AFTER_SWITCH = 0.1
Expand Down
26 changes: 15 additions & 11 deletions custom_components/ecodevices_rt2/sensors/sensor_counter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.sensor import SensorStateClass
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from pyecodevices_rt2 import Counter
from pyecodevices_rt2 import EcoDevicesRT2

from . import Sensor_EcoDevicesRT2
from ..const import CONF_DEVICE_CLASS
from ..const import CONF_STATE_CLASS


class Sensor_Counter(Sensor_EcoDevicesRT2):
Expand All @@ -16,32 +19,30 @@ def __init__(
device_config: dict,
ecort2: EcoDevicesRT2,
coordinator: DataUpdateCoordinator,
device_class: str,
suffix_name: str,
):
super().__init__(
hass, device_config, ecort2, coordinator, device_class, suffix_name
)
super().__init__(hass, device_config, ecort2, coordinator, suffix_name)
self.control = Counter(ecort2, self._id)


class Sensor_Counter_Index(Sensor_Counter):
def __init__(
self,
hass: HomeAssistant,
device_config: dict,
device_config_g: dict,
ecort2: EcoDevicesRT2,
coordinator: DataUpdateCoordinator,
):
device_class = SensorDeviceClass.ENERGY
if device_config.get("device_class"):
device_class = device_config.get("device_class")
device_config = dict(device_config_g)
if CONF_DEVICE_CLASS not in device_config:
device_config[CONF_DEVICE_CLASS] = SensorDeviceClass.ENERGY
device_config[CONF_STATE_CLASS] = SensorStateClass.TOTAL_INCREASING

super().__init__(
hass,
device_config,
ecort2,
coordinator,
device_class,
"Index",
)

Expand All @@ -55,16 +56,19 @@ class Sensor_Counter_Price(Sensor_Counter):
def __init__(
self,
hass: HomeAssistant,
device_config: dict,
device_config_g: dict,
ecort2: EcoDevicesRT2,
coordinator: DataUpdateCoordinator,
):
device_config = dict(device_config_g)
device_config[CONF_DEVICE_CLASS] = SensorDeviceClass.MONETARY
device_config[CONF_STATE_CLASS] = SensorStateClass.MEASUREMENT

super().__init__(
hass,
device_config,
ecort2,
coordinator,
SensorDeviceClass.MONETARY,
"Price",
)

Expand Down
110 changes: 42 additions & 68 deletions custom_components/ecodevices_rt2/sensors/sensor_ecodevicesrt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ..const import CONF_ICON_INSTANT
from ..const import CONF_ICON_PRICE
from ..const import CONF_ICON_TEMPERATURE
from ..const import CONF_STATE_CLASS
from ..const import CONF_UNIT_HUMIDITY
from ..const import CONF_UNIT_ILLUMINANCE
from ..const import CONF_UNIT_INDEX
Expand All @@ -28,9 +29,6 @@
from ..const import CONF_UNIT_TEMPERATURE
from ..device_ecodevicesrt2 import EcoDevicesRT2Device

# from homeassistant.components.sensor import CONF_STATE_CLASS
CONF_STATE_CLASS = "state_class"

_LOGGER = logging.getLogger(__name__)


Expand All @@ -52,85 +50,61 @@ def __init__(
device_config: dict,
ecort2: EcoDevicesRT2,
coordinator: DataUpdateCoordinator,
device_class: str = "",
suffix_name: str = "",
):
super().__init__(device_config, ecort2, coordinator, suffix_name)
self._allow_zero = device_config.get(CONF_ALLOW_ZERO, True)
self._state = None
self._state_class = device_config.get(CONF_STATE_CLASS)

if device_class == "" and self._device_class:
device_class = self._device_class

if device_class != "":
if device_class in DEVICE_CLASS_UNITS:
default_unit = list(DEVICE_CLASS_UNITS[device_class])[0]
if self._device_class != "":
if self._device_class in DEVICE_CLASS_UNITS:
default_unit = list(DEVICE_CLASS_UNITS[self._device_class])[0]
else:
default_unit = UNDEFINED
self._device_class = device_class
if device_class == SensorDeviceClass.MONETARY:
update_unit_icon(
self,
device_config,
CONF_UNIT_PRICE,
hass.config.currency,
CONF_ICON_PRICE,
)
self._state_class = SensorStateClass.TOTAL_INCREASING
elif device_class == SensorDeviceClass.ENERGY:
update_unit_icon(
self,
device_config,
CONF_UNIT_INDEX,
UnitOfEnergy.WATT_HOUR,
CONF_ICON_INDEX,
)
self._state_class = SensorStateClass.TOTAL_INCREASING
elif device_class == SensorDeviceClass.POWER:
update_unit_icon(
self,
device_config,
CONF_UNIT_INSTANT,
UnitOfPower.WATT,
CONF_ICON_INSTANT,
)
self._state_class = SensorStateClass.MEASUREMENT
elif device_class == SensorDeviceClass.TEMPERATURE:

conf_unit = UNDEFINED
conf_icon = UNDEFINED

if self._device_class == SensorDeviceClass.MONETARY:
default_unit = hass.config.currency
conf_unit = CONF_UNIT_PRICE
conf_icon = CONF_ICON_PRICE
elif self._device_class == SensorDeviceClass.TEMPERATURE:
if hass.config.units == METRIC_SYSTEM:
default_unit = UnitOfTemperature.CELSIUS
else:
default_unit = UnitOfTemperature.FAHRENHEIT
conf_unit = CONF_UNIT_TEMPERATURE
conf_icon = CONF_ICON_TEMPERATURE
elif self._device_class == SensorDeviceClass.HUMIDITY:
conf_unit = CONF_UNIT_HUMIDITY
conf_icon = CONF_ICON_HUMIDITY
elif self._device_class == SensorDeviceClass.ILLUMINANCE:
conf_unit = CONF_UNIT_ILLUMINANCE
conf_icon = CONF_ICON_ILLUMINANCE
elif self._device_class == SensorDeviceClass.ENERGY:
default_unit = UnitOfEnergy.WATT_HOUR # default_unit
conf_unit = CONF_UNIT_INDEX
conf_icon = CONF_ICON_INDEX
elif self._device_class == SensorDeviceClass.POWER:
default_unit = UnitOfPower.WATT # default_unit
conf_unit = CONF_UNIT_INSTANT
conf_icon = CONF_ICON_INSTANT
elif self._state_class == SensorStateClass.MEASUREMENT:
conf_unit = CONF_UNIT_INSTANT
conf_icon = CONF_ICON_INSTANT
elif self._state_class == SensorStateClass.TOTAL_INCREASING:
conf_unit = CONF_UNIT_INDEX
conf_icon = CONF_ICON_INDEX

update_unit_icon(
self,
device_config,
CONF_UNIT_TEMPERATURE,
default_unit,
CONF_ICON_TEMPERATURE,
)
self._state_class = SensorStateClass.MEASUREMENT
elif device_class == SensorDeviceClass.HUMIDITY:
update_unit_icon(
self,
device_config,
CONF_UNIT_HUMIDITY,
default_unit,
CONF_ICON_HUMIDITY,
)
self._state_class = SensorStateClass.MEASUREMENT
elif device_class == SensorDeviceClass.ILLUMINANCE:
update_unit_icon(
self,
device_config,
CONF_UNIT_ILLUMINANCE,
default_unit,
CONF_ICON_ILLUMINANCE,
)
self._state_class = SensorStateClass.MEASUREMENT
else:
if not self._unit_of_measurement:
self._unit_of_measurement = default_unit
update_unit_icon(
self,
device_config,
conf_unit,
default_unit,
conf_icon,
)

@property
def state(self) -> str:
Expand Down
Loading

0 comments on commit 756cbfe

Please sign in to comment.