Skip to content

Commit

Permalink
Code Cleanup, improve naming, parameter filtering (#59)
Browse files Browse the repository at this point in the history
* move get_tls_context to helper

* add mote type converter

* update requirements

* cleanup const

* Use flags from parameter_data

* cleanup const

* improve entity naming

* fix channel assignement for dimmers
  • Loading branch information
SukramJ authored Nov 27, 2021
1 parent 1682043 commit b0fb2eb
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 308 deletions.
10 changes: 10 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Version 0.0.12 (2021-11-27)
- Add more type converter
- Move get_tls_context to helper
- Update requirements
- Cleanup constants
- Use flags from parameter_data
- Add wildcard start to exclude parameters that start with word
- Fix channel assignement for dimmers
- Fix entity name: add channel only if a parameter name exists is in multiple channels of the device.

Version 0.0.11 (2021-11-26)
- Fix: cover open/close default values to float
- Fix: add missing async/await
Expand Down
41 changes: 33 additions & 8 deletions hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
DATA_NO_LOAD,
DATA_NO_SAVE,
DATA_SAVE_SUCCESS,
DEFAULT_CONNECT,
DEFAULT_ENCODING,
DEFAULT_JSON_PORT,
DEFAULT_NAME,
DEFAULT_PASSWORD,
DEFAULT_TLS,
DEFAULT_USERNAME,
Expand Down Expand Up @@ -63,7 +60,6 @@ def __init__(self, central_config):
self.enable_virtual_channels = self.central_config.enable_virtual_channels
self.host = self.central_config.host
self.json_port = self.central_config.json_port
self.connect = self.central_config.connect
self.password = self.central_config.password
if self.password is None:
self.username = None
Expand All @@ -76,6 +72,8 @@ def __init__(self, central_config):
# Caches for CCU data
# {interface_id, {address, paramsets}}
self.paramsets_cache = {}

self.address_parameter_cache = {}
# {interface_id, {address, name}}
self.names_cache = {}
# {interface_id, {counter, device}}
Expand Down Expand Up @@ -112,6 +110,7 @@ def __init__(self, central_config):

INSTANCES[self.instance_name] = self
self._load_caches()
self.init_address_parameter_list()
self._connection_checker = ConnectionChecker(self)
self.hub = None

Expand All @@ -126,6 +125,33 @@ async def init_hub(self):
else:
self.hub = HmDummyHub(self)

def init_address_parameter_list(self):
"""Initialize an address/parameter list to identify if a parameter name exists is in multiple channels."""
for device_paramsets in self.paramsets_cache.values():
for address, paramsets in device_paramsets.items():
if ":" not in address:
continue
d_address = address.split(":")[0]
p_channel = address.split(":")[1]

for paramset in paramsets.values():
for parameter in paramset:
if (d_address, parameter) not in self.address_parameter_cache:
self.address_parameter_cache[(d_address, parameter)] = []
self.address_parameter_cache[(d_address, parameter)].append(
p_channel
)

def has_multiple_channels(self, address, parameter) -> bool:
"""Check if parameter is in multiple channels per device."""
if ":" not in address:
return False
d_address = address.split(":")[0]
channels = self.address_parameter_cache.get((d_address, parameter))
if channels:
return len(set(channels)) > 1
return False

@property
def version(self):
"""Return the version of the backend."""
Expand Down Expand Up @@ -491,6 +517,7 @@ def _save_paramsets():
json.dump(self.paramsets_cache, fptr)
return DATA_SAVE_SUCCESS

self.init_address_parameter_list()
return await self.async_add_executor_job(_save_paramsets)

def load_paramsets(self):
Expand Down Expand Up @@ -674,17 +701,16 @@ def __init__(
entry_id,
loop,
xml_rpc_server: xml_rpc.XMLRPCServer,
name=DEFAULT_NAME,
name,
host=LOCALHOST,
username=DEFAULT_USERNAME,
password=DEFAULT_PASSWORD,
tls=DEFAULT_TLS,
verify_tls=DEFAULT_VERIFY_TLS,
client_session=None,
connect=DEFAULT_CONNECT,
callback_host=None,
callback_port=None,
json_port=DEFAULT_JSON_PORT,
json_port=None,
json_tls=DEFAULT_TLS,
enable_virtual_channels=False,
enable_sensors_for_own_system_variables=False,
Expand All @@ -699,7 +725,6 @@ def __init__(
self.tls = tls
self.verify_tls = verify_tls
self.client_session = client_session
self.connect = connect
self.callback_host = callback_host
self.callback_port = callback_port
self.json_port = json_port
Expand Down
10 changes: 1 addition & 9 deletions hahomematic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
BACKEND_CCU,
BACKEND_HOMEGEAR,
BACKEND_PYDEVCCU,
DEFAULT_NAME,
DEFAULT_PATH,
HM_VIRTUAL_REMOTES,
PORT_RFD,
PROXY_DE_INIT_FAILED,
PROXY_DE_INIT_SKIPPED,
PROXY_DE_INIT_SUCCESS,
PROXY_INIT_FAILED,
PROXY_INIT_SKIPPED,
PROXY_INIT_SUCCESS,
RELEVANT_PARAMSETS,
)
Expand Down Expand Up @@ -128,9 +126,6 @@ async def proxy_init(self) -> int:
To receive events the proxy has to tell the CCU / Homegear
where to send the events. For that we call the init-method.
"""
if not self.central.connect:
_LOGGER.debug("proxy_init: Skipping init for %s", self.name)
return PROXY_INIT_SKIPPED
if self.central is None:
_LOGGER.warning("proxy_init: Local central_unit missing for %s", self.name)
self.time_initialized = 0
Expand All @@ -156,9 +151,6 @@ async def proxy_de_init(self) -> int:
"""
if self.json_rpc_session.is_activated:
await self.json_rpc_session.logout()
if not self.central.connect:
_LOGGER.debug("proxy_de_init: Skipping de-init for %s", self.name)
return PROXY_DE_INIT_SKIPPED
if self.central is None:
_LOGGER.warning("proxy_de_init: Local central missing for %s", self.name)
return PROXY_DE_INIT_FAILED
Expand Down Expand Up @@ -685,7 +677,7 @@ class ClientConfig:
def __init__(
self,
central,
name=DEFAULT_NAME,
name,
port=PORT_RFD,
path=DEFAULT_PATH,
callback_host=None,
Expand Down
Loading

0 comments on commit b0fb2eb

Please sign in to comment.