Skip to content

Commit

Permalink
Support when pychonet EPC_CODE name is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nao-pon committed Feb 13, 2024
1 parent 1d23eec commit 59e18a9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
36 changes: 26 additions & 10 deletions custom_components/echonetlite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,35 @@
]


def get_name_by_epc_code(
jgc: int, jcc: int, code: int, unknown: str | None = None
) -> str:
if code in EPC_SUPER:
return EPC_SUPER[code]
else:
if unknown == None:
unknown = f"({code})"
name = EPC_CODE.get(jgc, {}).get(jcc, {}).get(code, None)
if name == None:
_code = f"[{hex(jgc)}({jgc})-{hex(jcc)}({jcc})-{hex(code)}({code})]"
_LOGGER.warning(
f"{_code} - Unable to resolve the item name. "
+ f"Please report the unknown code {_code} at the issue tracker on GitHub!"
)
if unknown == None:
name = f"({code})"
else:
name = unknown
return name


def polling_update_debug_log(values, eojgc, eojcc):
debug_log = f"\nECHONETlite polling update data:\n"
for value in list(values.keys()):
if value in EPC_SUPER:
debug_log = (
debug_log
+ f" - {EPC_SUPER[value]} {value:#x}({value}): {values[value]}\n"
)
elif value in EPC_CODE[eojgc][eojcc]:
debug_log = (
debug_log
+ f" - {EPC_CODE[eojgc][eojcc][value]} {value:#x}({value}): {values[value]}\n"
)
debug_log = (
debug_log
+ f" - {get_name_by_epc_code(eojgc,eojcc,value)} {value:#x}({value}): {values[value]}\n"
)
return debug_log


Expand Down
5 changes: 2 additions & 3 deletions custom_components/echonetlite/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
)
from homeassistant.exceptions import InvalidStateError
from homeassistant.components.number import NumberEntity
from pychonet.lib.epc import EPC_CODE
from pychonet.lib.eojx import EOJX_CLASS
from . import get_unit_by_devise_class
from . import get_name_by_epc_code, get_unit_by_devise_class
from .const import (
DOMAIN,
CONF_FORCE_POLLING,
Expand Down Expand Up @@ -59,7 +58,7 @@ def __init__(self, hass, connector, config, code, options, name=None):
self._connector._instance._host
]
self._attr_icon = options.get(CONF_ICON, None)
self._attr_name = f"{config.title} {EPC_CODE[self._connector._eojgc][self._connector._eojcc][self._code]}"
self._attr_name = f"{config.title} {get_name_by_epc_code(self._connector._eojgc, self._connector._eojcc,self._code)}"
self._attr_unique_id = (
f"{self._connector._uidi}-{self._code}"
if self._connector._uidi
Expand Down
4 changes: 2 additions & 2 deletions custom_components/echonetlite/select.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
from homeassistant.const import CONF_ICON
from homeassistant.components.select import SelectEntity
from . import get_name_by_epc_code
from .const import (
DOMAIN,
CONF_FORCE_POLLING,
ENL_OP_CODES,
CONF_ICONS,
TYPE_SELECT,
)
from pychonet.lib.epc import EPC_CODE
from pychonet.lib.eojx import EOJX_CLASS
from pychonet.lib.epc_functions import _swap_dict

Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(self, hass, connector, config, code, options, name=None):
if self._connector._user_options[code] is not False:
self._attr_options = self._connector._user_options[code]
self._attr_current_option = self._connector._update_data.get(self._code)
self._attr_name = f"{config.title} {EPC_CODE[self._connector._eojgc][self._connector._eojcc][self._code]}"
self._attr_name = f"{config.title} {get_name_by_epc_code(self._connector._eojgc,self._connector._eojcc,self._code)}"
self._attr_unique_id = (
f"{self._connector._uidi}-{self._code}"
if self._connector._uidi
Expand Down
15 changes: 4 additions & 11 deletions custom_components/echonetlite/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

from pychonet.GeneralLighting import ENL_BRIGHTNESS, ENL_COLOR_TEMP

from pychonet.lib.epc import EPC_CODE, EPC_SUPER
from pychonet.lib.eojx import EOJX_CLASS
from pychonet.lib.epc_functions import _hh_mm

from . import get_unit_by_devise_class
from . import get_name_by_epc_code, get_unit_by_devise_class
from .const import (
DOMAIN,
ENL_OP_CODES,
Expand Down Expand Up @@ -274,15 +273,9 @@ def __init__(self, connector, op_code, attributes, name=None, hass=None) -> None
self._attr_state_class = self._sensor_attributes.get(CONF_STATE_CLASS)

# Create name based on sensor description from EPC codes, super class codes or fallback to using the sensor type
self._attr_name = f"{name}"
if self._op_code in EPC_CODE[self._eojgc][self._eojcc].keys():
self._attr_name = (
f"{name} {EPC_CODE[self._eojgc][self._eojcc][self._op_code]}"
)
elif self._op_code in EPC_SUPER.keys():
self._attr_name = f"{name} {EPC_SUPER[self._op_code]}"
elif self._attr_device_class:
self._attr_name = f"{name} {self._attr_device_class}"
self._attr_name = get_name_by_epc_code(
self._eojgc, self._eojcc, self._op_code, self._attr_device_class
)

if "dict_key" in _attr_keys:
self._attr_unique_id += f'-{self._sensor_attributes["dict_key"]}'
Expand Down
4 changes: 2 additions & 2 deletions custom_components/echonetlite/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from homeassistant.const import CONF_ICON, CONF_SERVICE_DATA, CONF_NAME
from homeassistant.components.switch import SwitchEntity
from . import get_name_by_epc_code
from .const import (
DOMAIN,
ENL_OP_CODES,
Expand All @@ -16,7 +17,6 @@
ENL_STATUS,
CONF_FORCE_POLLING,
)
from pychonet.lib.epc import EPC_CODE
from pychonet.lib.eojx import EOJX_CLASS

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(self, hass, connector, config, code, options, name=None):
hex(self._options[CONF_SERVICE_DATA][DATA_STATE_ON])[2:],
]
self._from_number = True if options.get(TYPE_NUMBER) else False
self._attr_name = f"{config.title} {EPC_CODE[self._connector._eojgc][self._connector._eojcc][self._code]}"
self._attr_name = f"{config.title} {get_name_by_epc_code(self._connector._eojgc,self._connector._eojcc,self._code)}"
self._attr_icon = options.get(CONF_ICON)
self._attr_unique_id = (
f"{self._connector._uidi}-{self._code}"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/echonetlite/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from homeassistant.const import CONF_ICON
from homeassistant.components.time import TimeEntity
from homeassistant.exceptions import InvalidStateError
from . import get_name_by_epc_code
from .const import (
DOMAIN,
CONF_FORCE_POLLING,
ENL_OP_CODES,
TYPE_TIME,
)
from pychonet.lib.epc import EPC_CODE
from pychonet.lib.eojx import EOJX_CLASS
from pychonet.lib.epc_functions import _hh_mm

Expand Down Expand Up @@ -58,7 +58,7 @@ def __init__(self, hass, connector, config, code, options, device_name=None):
self._connector._instance._host
]
self._attr_icon = options.get(CONF_ICON, None)
self._attr_name = f"{config.title} {EPC_CODE[self._connector._eojgc][self._connector._eojcc][self._code]}"
self._attr_name = f"{config.title} {get_name_by_epc_code(self._connector._eojgc,self._connector._eojcc,self._code)}"
self._attr_unique_id = (
f"{self._connector._uidi}-{self._code}"
if self._connector._uidi
Expand Down

0 comments on commit 59e18a9

Please sign in to comment.