Skip to content

Commit

Permalink
Add helper for address / Fixes for Hub init (#69)
Browse files Browse the repository at this point in the history
- Add helper for address
- Fixes for Hub init
  • Loading branch information
SukramJ authored Dec 12, 2021
1 parent 9299080 commit 571ca9b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 deletions.
6 changes: 5 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Version 0.0.18 (2021-12-12)
Version 0.0.19 (2021-12-12)
- Add helper for address
- Fixes for Hub init

Version 0.0.18 (2021-12-11)
- Add type hints based on HA coding guidelines
- Rename device_description to entity_definition
- Send alarm event on value change
Expand Down
28 changes: 12 additions & 16 deletions hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from hahomematic.data import INSTANCES
from hahomematic.device import HmDevice, create_devices
from hahomematic.entity import BaseEntity, GenericEntity
from hahomematic.helpers import get_device_address, get_device_channel
from hahomematic.hub import HmDummyHub, HmHub
from hahomematic.json_rpc_client import JsonRpcAioHttpClient
from hahomematic.proxy import NoConnection
Expand Down Expand Up @@ -112,7 +113,7 @@ def __init__(self, central_config: CentralConfig):
self._load_caches()
self.init_address_parameter_list()
self._connection_checker = ConnectionChecker(self)
self.hub: HmHub | HmDummyHub = self.create_hub()
self.hub: HmHub | HmDummyHub | None = None

def create_hub(self) -> HmHub | HmDummyHub:
"""Create the hub."""
Expand All @@ -128,6 +129,7 @@ def create_hub(self) -> HmHub | HmDummyHub:

async def init_hub(self) -> None:
"""Init the hub."""
self.hub = self.create_hub()
if isinstance(self.hub, HmHub):
await self.hub.fetch_data()

Expand All @@ -137,23 +139,23 @@ def init_address_parameter_list(self) -> None:
for address, paramsets in device_paramsets.items():
if ":" not in address:
continue
d_address = address.split(":")[0]
p_channel = int(address.split(":")[1])
d_address = get_device_address(address)

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
get_device_channel(address)
)

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

Expand Down Expand Up @@ -384,8 +386,7 @@ async def press_virtual_remote_key(self, address: str, parameter: str) -> None:
HM_VIRTUAL_REMOTE_HMIP.upper(), HM_VIRTUAL_REMOTE_HMIP
)

device_address = address.split(":")[0]
if virtual_remote := self._get_virtual_remote(device_address):
if virtual_remote := self._get_virtual_remote(get_device_address(address)):
if virtual_remote_channel := virtual_remote.action_events.get(
(address, parameter)
):
Expand Down Expand Up @@ -426,19 +427,14 @@ def get_hm_entity_by_parameter(
) -> GenericEntity | None:
"""Get entity by address and parameter."""
if ":" in address:
device_address = address.split(":")[0]
if device := self.hm_devices.get(device_address):
if device := self.hm_devices.get(get_device_address(address)):
if entity := device.entities.get((address, parameter)):
return entity
return None

def has_address(self, address: str) -> bool:
"""Check if address is handled by central_unit."""
device_address = address
if ":" in address:
device_address = device_address.split(":")[0]

return self.hm_devices.get(device_address) is not None
return self.hm_devices.get(get_device_address(address)) is not None

def get_all_parameters(self) -> list[str]:
"""Return all parameters"""
Expand Down Expand Up @@ -724,7 +720,7 @@ def handle_device_descriptions(
if ":" not in address and address not in central.devices[interface_id]:
central.devices[interface_id][address] = {}
if ":" in address:
main, _ = address.split(":")
main = get_device_address(address)
if main not in central.devices[interface_id]:
central.devices[interface_id][main] = {}
central.devices[interface_id][main][address] = {}
Expand Down
32 changes: 8 additions & 24 deletions hahomematic/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
HM_ENTITY_UNIT_REPLACE,
INIT_DATETIME,
OPERATION_READ,
TYPE_BOOL,
TYPE_FLOAT,
TYPE_INTEGER,
TYPE_STRING,
HmEventType,
HmPlatform,
)
import hahomematic.device as hm_device
import hahomematic.devices.entity_definition as hm_entity_definition
from hahomematic.helpers import get_custom_entity_name, get_entity_name
from hahomematic.helpers import (
get_custom_entity_name,
get_device_address,
get_entity_name,
)
import hahomematic.proxy as hm_proxy

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -258,20 +258,6 @@ def hmtype(self) -> str | None:
"""Return the homematic type."""
return self._type

def _convert_value(self, value: Any | None) -> Any | None:
"""Convert value to a given hm_type."""
if value is None:
return None
if self._type == TYPE_BOOL:
return bool(value)
if self._type == TYPE_FLOAT:
return float(value)
if self._type == TYPE_INTEGER:
return int(value)
if self._type == TYPE_STRING:
return str(value)
return value

async def send_value(self, value: Any) -> None:
"""send value to ccu."""
try:
Expand Down Expand Up @@ -315,8 +301,6 @@ def __init__(
CallbackEntity.__init__(self)

self._state: ParameterType | None = None
# if self._type == TYPE_ACTION:
# self._state = False

# Subscribe for all events of this device
if (
Expand Down Expand Up @@ -674,7 +658,7 @@ def __init__(

def get_event_data(self, value: Any = None) -> dict[str, Any]:
"""Get the event_data."""
address = self.address.split(":")[0]
address = get_device_address(self.address)
click_type = self.parameter.lower()
return {
ATTR_INTERFACE_ID: self._interface_id,
Expand Down Expand Up @@ -774,7 +758,7 @@ def get_event_data(self, value: Any = None) -> dict[str, Any]:
"""Get the event_data."""
return {
ATTR_INTERFACE_ID: self._interface_id,
ATTR_ADDRESS: self.address,
ATTR_ADDRESS: get_device_address(self.address),
ATTR_PARAMETER: self.parameter,
ATTR_VALUE: value,
}
Expand All @@ -795,7 +779,7 @@ def fire_event(self, value: bool | None) -> None:
return None
if self.parameter == EVENT_UN_REACH:
self._device.update_device(self.unique_id)
return None
# no return here. Event should also be fired for persistent notification.

if callable(self._central.callback_ha_event):
self._central.callback_ha_event(
Expand Down
14 changes: 14 additions & 0 deletions hahomematic/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,17 @@ def get_tls_context(verify_tls: bool) -> ssl.SSLContext:
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
return ssl_context


def get_device_address(address: str) -> str:
"""Return the device part of an address"""
if ":" in address:
return address.split(":")[0]
return address


def get_device_channel(address: str) -> int:
"""Return the channel part of an address"""
if ":" not in address:
raise Exception("Address has no channel part.")
return int(address.split(":")[1])
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def readme():
with open("README.md") as fptr:
return fptr.read()


package_data = {
'foopkg': ['py.typed'],
},
PACKAGE_NAME = "hahomematic"
HERE = os.path.abspath(os.path.dirname(__file__))
VERSION = "0.0.18"
VERSION = "0.0.19"

PACKAGES = find_packages(exclude=["tests", "tests.*", "dist", "build"])

Expand All @@ -30,7 +31,7 @@ def readme():
author_email="[email protected]",
description="Homematic interface for Home Assistant",
packages=PACKAGES,
package_data={ 'hahomematic': ['py.typed'],},
package_data={'hahomematic': ['py.typed']},
zip_safe=False,
platforms="any",
python_requires=">=3.8",
Expand Down

0 comments on commit 571ca9b

Please sign in to comment.