Skip to content

Commit

Permalink
Automatically assigning icons using device_class and optimizing impor…
Browse files Browse the repository at this point in the history
…t syntax

And enbug fixes.
  • Loading branch information
nao-pon committed Feb 3, 2024
1 parent d401b17 commit 58a8254
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 181 deletions.
64 changes: 58 additions & 6 deletions custom_components/echonetlite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.util import Throttle
from homeassistant.const import Platform
from homeassistant.const import (
Platform,
PERCENTAGE,
UnitOfPower,
UnitOfTemperature,
UnitOfEnergy,
UnitOfVolume,
UnitOfElectricCurrent,
UnitOfElectricPotential,
)
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.number import NumberDeviceClass
from .const import (
DOMAIN,
USER_OPTIONS,
TEMP_OPTIONS,
CONF_FORCE_POLLING,
CONF_BATCH_SIZE_MAX,
MISC_OPTIONS,
)
Expand Down Expand Up @@ -47,8 +57,6 @@
ENL_HVAC_ROOM_TEMP,
ENL_HVAC_SILENT_MODE,
ENL_HVAC_OUT_TEMP,
ENL_HVAC_HUMIDIFIER_STATE,
ENL_HVAC_HUMIDIFIER_VALUE,
)

from pychonet.DistributionPanelMeter import (
Expand All @@ -65,7 +73,6 @@

from pychonet.LowVoltageSmartElectricEnergyMeter import (
ENL_LVSEEM_COEF,
ENL_LVSEEM_DIGITS,
ENL_LVSEEM_ENG_UNIT,
ENL_LVSEEM_ENG_NOR,
ENL_LVSEEM_ENG_REV,
Expand Down Expand Up @@ -190,6 +197,52 @@ def polling_update_debug_log(values, eojgc, eojcc):
return debug_log


def get_unit_by_devise_class(device_class: str) -> str | None:
if (
device_class == SensorDeviceClass.TEMPERATURE
or device_class == NumberDeviceClass.TEMPERATURE
):
unit = UnitOfTemperature.CELSIUS
elif (
device_class == SensorDeviceClass.ENERGY
or device_class == NumberDeviceClass.ENERGY
):
unit = UnitOfEnergy.WATT_HOUR
elif (
device_class == SensorDeviceClass.POWER
or device_class == NumberDeviceClass.POWER
):
unit = UnitOfPower.WATT
elif (
device_class == SensorDeviceClass.CURRENT
or device_class == NumberDeviceClass.CURRENT
):
unit = UnitOfElectricCurrent.AMPERE
elif (
device_class == SensorDeviceClass.VOLTAGE
or device_class == NumberDeviceClass.VOLTAGE
):
unit = UnitOfElectricPotential.VOLT
elif (
device_class == SensorDeviceClass.HUMIDITY
or device_class == SensorDeviceClass.BATTERY
or device_class == NumberDeviceClass.HUMIDITY
or device_class == NumberDeviceClass.BATTERY
):
unit = PERCENTAGE
elif device_class == SensorDeviceClass.GAS or device_class == NumberDeviceClass.GAS:
unit = UnitOfVolume.CUBIC_METERS
elif (
device_class == SensorDeviceClass.WATER
or device_class == NumberDeviceClass.WATER
):
unit = UnitOfVolume.CUBIC_METERS
else:
unit = None

return unit


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.async_on_unload(entry.add_update_listener(update_listener))
host = None
Expand Down Expand Up @@ -409,7 +462,6 @@ class ECHONETConnector:
def __init__(self, instance, hass, entry):
self.hass = hass
self._host = instance["host"]
self._instance = None
self._eojgc = instance["eojgc"]
self._eojcc = instance["eojcc"]
self._eojci = instance["eojci"]
Expand Down
22 changes: 13 additions & 9 deletions custom_components/echonetlite/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
ENL_HVAC_ROOM_TEMP,
ENL_HVAC_SILENT_MODE,
FAN_SPEED,
MODES,
SILENT_MODE,
)

Expand All @@ -36,8 +35,9 @@
from homeassistant.const import (
ATTR_TEMPERATURE,
PRECISION_WHOLE,
UnitOfTemperature,
)
from .const import DATA_STATE_ON, DOMAIN, SILENT_MODE_OPTIONS, OPTION_HA_UI_SWING
from .const import DATA_STATE_ON, DOMAIN, OPTION_HA_UI_SWING

_LOGGER = logging.getLogger(__name__)

Expand All @@ -64,11 +64,7 @@ async def async_setup_entry(hass, config_entry, async_add_devices):
if (
entity["instance"]["eojgc"] == 0x01 and entity["instance"]["eojcc"] == 0x30
): # Home Air Conditioner
entities.append(
EchonetClimate(
config_entry.title, entity["echonetlite"], hass.config.units
)
)
entities.append(EchonetClimate(config_entry.title, entity["echonetlite"]))
async_add_devices(entities, True)

platform = entity_platform.async_get_current_platform()
Expand All @@ -89,7 +85,12 @@ class EchonetClimate(ClimateEntity):
_attr_translation_key = DOMAIN

def __init__(
self, name, connector, units: UnitSystem, fan_modes=None, swing_vert=None
self,
name,
connector,
units: UnitSystem | None = None,
fan_modes=None,
swing_vert=None,
):
"""Initialize the climate device."""
self._name = name
Expand All @@ -98,7 +99,10 @@ def __init__(
self._uid = (
self._connector._uidi if self._connector._uidi else self._connector._uid
)
self._unit_of_measurement = units.temperature_unit
# The temperature unit of echonet lite is defined as Celsius.
# Set temperature_unit setting to Celsius,
# HA's automatic temperature unit conversion function works correctly.
self._unit_of_measurement = UnitOfTemperature.CELSIUS
self._precision = 1.0
self._target_temperature_step = 1
self._support_flags = SUPPORT_FLAGS
Expand Down
Loading

0 comments on commit 58a8254

Please sign in to comment.