Skip to content

Commit

Permalink
Fix unique_id for heating_groups (#128)
Browse files Browse the repository at this point in the history
* Remove domain const from hahomematic

* Use domain name as base folder name

* update tests

* make unique_id for heating_groups

* Remove dedicated json tls option

* update changelog
  • Loading branch information
SukramJ authored Jan 4, 2022
1 parent 4031ca7 commit e8774e0
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 37 deletions.
6 changes: 6 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 0.13.0 (2022-01-04)
- Remove dedicated json tls option
- Fix unique_id for heating_groups
- Use domain name as base folder name
- Remove domain const from hahomematic

Version 0.12.0 (2022-01-03)
- Split number to integer and float

Expand Down
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def hacallback(self, eventtype, event_data):

async def example_run(self):
self.central = await CentralConfig(
domain="hahm",
name="ccu-dev",
loop=asyncio.get_running_loop(),
xml_rpc_server=register_xml_rpc_server(),
Expand Down
2 changes: 2 additions & 0 deletions example_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def hacallback(self, eventtype, event_data):

async def example_run(self):
self.central_1 = await CentralConfig(
domain="hahm",
name="ccu-dev",
loop=asyncio.get_running_loop(),
xml_rpc_server=register_xml_rpc_server(),
Expand All @@ -91,6 +92,7 @@ async def example_run(self):
option_enable_virtual_channels=True,
).get_central()
self.central_2 = await CentralConfig(
domain="hahm",
name="ccu-2-dev",
loop=asyncio.get_running_loop(),
xml_rpc_server=register_xml_rpc_server(),
Expand Down
17 changes: 11 additions & 6 deletions hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
FILE_DEVICES,
FILE_NAMES,
FILE_PARAMSETS,
HA_DOMAIN,
HM_VIRTUAL_REMOTE_HM,
HM_VIRTUAL_REMOTE_HMIP,
LOCALHOST,
Expand Down Expand Up @@ -62,6 +61,7 @@ class CentralUnit:
def __init__(self, central_config: CentralConfig):
_LOGGER.debug("CentralUnit.__init__")
self.central_config: CentralConfig = central_config
self._domain = self.central_config.domain

self.instance_name: str = self.central_config.name
self._available: bool = True
Expand Down Expand Up @@ -107,6 +107,11 @@ def __init__(self, central_config: CentralConfig):
self._connection_checker = ConnectionChecker(self)
self.hub: HmHub | HmDummyHub | None = None

@property
def domain(self) -> str:
"""Return the domain."""
return self._domain

def create_hub(self) -> HmHub | HmDummyHub:
"""Create the hub."""
hub: HmHub | HmDummyHub
Expand Down Expand Up @@ -154,7 +159,7 @@ def device_url(self) -> str:
def device_info(self) -> dict[str, Any]:
"""Return central specific attributes."""
return {
"identifiers": {(HA_DOMAIN, self.instance_name)},
"identifiers": {(self._domain, self.instance_name)},
"name": self.instance_name,
"manufacturer": MANUFACTURER,
"model": self.model,
Expand Down Expand Up @@ -535,6 +540,7 @@ def __init__(
self,
loop: asyncio.AbstractEventLoop,
xml_rpc_server: xml_rpc.XmlRpcServer,
domain: str,
name: str,
host: str = LOCALHOST,
username: str = DEFAULT_USERNAME,
Expand All @@ -545,12 +551,12 @@ def __init__(
callback_host: str | None = None,
callback_port: int | None = None,
json_port: int | None = None,
json_tls: bool = DEFAULT_TLS,
option_enable_virtual_channels: bool = False,
option_enable_sensors_for_system_variables: bool = False,
):
self.loop = loop
self.xml_rpc_server = xml_rpc_server
self.domain = domain
self.name = name
self.host = host
self.username = username
Expand All @@ -561,7 +567,6 @@ def __init__(
self.callback_host = callback_host
self.callback_port = callback_port
self.json_port = json_port
self.json_tls = json_tls
self.option_enable_virtual_channels = option_enable_virtual_channels
self.option_enable_sensors_for_system_variables = (
option_enable_sensors_for_system_variables
Expand All @@ -571,7 +576,7 @@ def __init__(
def device_url(self) -> str:
"""Return the required url."""
url = "http://"
if self.json_tls:
if self.tls:
url = "https://"
url = f"{url}{self.host}"
if self.json_port:
Expand All @@ -595,7 +600,7 @@ def __init__(
cache_dict: dict[str, Any],
):
self._central = central
self._cache_dir = config.CACHE_DIR
self._cache_dir = f"{self._central.domain}/cache"
self._filename = f"{self._central.instance_name}_{filename}"
self._cache_dict = cache_dict

Expand Down
5 changes: 0 additions & 5 deletions hahomematic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

from hahomematic.const import DEFAULT_INIT_TIMEOUT, DEFAULT_TIMEOUT

BASE_DIR = "hahm/"
CACHE_DIR = f"{BASE_DIR}cache"
DEVICE_DESCRIPTIONS_DIR = f"{BASE_DIR}export_device_descriptions"
PARAMSET_DESCRIPTIONS_DIR = f"{BASE_DIR}export_paramset_descriptions"

CONNECTION_CHECKER_INTERVAL = 30
INIT_TIMEOUT = DEFAULT_INIT_TIMEOUT
TIMEOUT = DEFAULT_TIMEOUT
3 changes: 1 addition & 2 deletions hahomematic/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from enum import Enum

DEFAULT_ENCODING = "UTF-8"
HA_DOMAIN = "hahm"
MANUFACTURER = "eQ-3"
INIT_DATETIME = datetime.strptime("01.01.1970 00:00:00", "%d.%m.%Y %H:%M:%S")
LOCALHOST = "localhost"
Expand Down Expand Up @@ -295,7 +294,7 @@ def __str__(self) -> str:


class HmEventType(Enum):
"""Enum with hahm event types."""
"""Enum with hahomematic event types."""

ALARM = "homematic.alarm"
KEYPRESS = "homematic.keypress"
Expand Down
20 changes: 15 additions & 5 deletions hahomematic/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
BUTTON_ACTIONS,
CLICK_EVENTS,
FLAG_INTERAL,
HA_DOMAIN,
HH_EVENT_DEVICES_CREATED,
HM_VIRTUAL_REMOTES,
IDENTIFIERS_SEPARATOR,
Expand Down Expand Up @@ -271,15 +270,15 @@ def device_info(self) -> dict[str, Any]:
return {
"identifiers": {
(
HA_DOMAIN,
self._central.domain,
f"{self._device_address}{IDENTIFIERS_SEPARATOR}{self._interface_id}",
)
},
"name": self.name,
"manufacturer": MANUFACTURER,
"model": self.device_type,
"sw_version": self.firmware,
"via_device": (HA_DOMAIN, self._central.instance_name),
"via_device": (self._central.domain, self._central.instance_name),
}

@property
Expand Down Expand Up @@ -397,6 +396,8 @@ def create_button(
) -> HmButton | None:
"""Create the buttons associated to this device"""
unique_id = generate_unique_id(
domain=self._central.domain,
instance_name=self._central.instance_name,
address=channel_address,
parameter=parameter,
prefix=f"button_{self._central.instance_name}",
Expand Down Expand Up @@ -427,7 +428,11 @@ def create_event(
self._central.entity_event_subscriptions[(channel_address, parameter)] = []

unique_id = generate_unique_id(
channel_address, parameter, f"event_{self._central.instance_name}"
domain=self._central.domain,
instance_name=self._central.instance_name,
address=channel_address,
parameter=parameter,
prefix=f"event_{self._central.instance_name}",
)

_LOGGER.debug(
Expand Down Expand Up @@ -485,7 +490,12 @@ def create_entity(
if (channel_address, parameter) not in self._central.entity_event_subscriptions:
self._central.entity_event_subscriptions[(channel_address, parameter)] = []

unique_id = generate_unique_id(channel_address, parameter)
unique_id = generate_unique_id(
domain=self._central.domain,
instance_name=self._central.instance_name,
address=channel_address,
parameter=parameter,
)
if unique_id in self._central.hm_entities:
_LOGGER.debug("create_entity: Skipping %s (already exists)", unique_id)
return None
Expand Down
6 changes: 5 additions & 1 deletion hahomematic/devices/entity_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,11 @@ def _create_entities(
) -> list[hm_entity.BaseEntity]:
"""Create custom entities."""
entities: list[hm_entity.BaseEntity] = []
unique_id = generate_unique_id(f"{device_address}:{channel_no}")
unique_id = generate_unique_id(
domain=device.central.domain,
instance_name=device.central.instance_name,
address=f"{device_address}:{channel_no}",
)
if unique_id in device.central.hm_entities:
_LOGGER.debug("make_custom_entity: Skipping %s (already exists)", unique_id)
return entities
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,5 +877,5 @@ class NoneTypeEntity:

value: Any = None

def send_value(self, value) -> None:
def send_value(self, value: Any) -> None:
"""Dummy method."""
12 changes: 8 additions & 4 deletions hahomematic/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
ATTR_NAME,
ATTR_TYPE,
ATTR_VALUE,
HA_DOMAIN,
)
import hahomematic.devices.entity_definition as hm_entity_definition

Expand All @@ -31,7 +30,11 @@ class ClientException(Exception):


def generate_unique_id(
address: str, parameter: str | None = None, prefix: str | None = None
domain: str,
instance_name: str,
address: str,
parameter: str | None = None,
prefix: str | None = None,
) -> str:
"""
Build unique id from address and parameter.
Expand All @@ -42,8 +45,9 @@ def generate_unique_id(

if prefix:
unique_id = f"{prefix}_{unique_id}"

return f"{HA_DOMAIN}_{unique_id}".lower()
if address.startswith("INT000"):
return f"{domain}_{instance_name}_{unique_id}".lower()
return f"{domain}_{unique_id}".lower()


def make_http_credentials(
Expand Down
22 changes: 19 additions & 3 deletions hahomematic/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ class HmSystemVariable(BaseHubEntity):

def __init__(self, central: hm_central.CentralUnit, name: str, value: Any):
self._hub: HmHub | HmDummyHub | None = central.hub
unique_id = generate_unique_id(central.instance_name, name, prefix="hub")
unique_id = generate_unique_id(
domain=central.domain,
instance_name=central.instance_name,
address=central.instance_name,
parameter=name,
prefix="hub",
)
super().__init__(central=central, unique_id=unique_id, name=name, value=value)

@property
Expand Down Expand Up @@ -165,7 +171,12 @@ class HmHub(BaseHubEntity):

def __init__(self, central: hm_central.CentralUnit, use_entities: bool = False):
"""Initialize HomeMatic hub."""
unique_id: str = generate_unique_id(central.instance_name, prefix="hub")
unique_id: str = generate_unique_id(
domain=central.domain,
instance_name=central.instance_name,
address=central.instance_name,
prefix="hub",
)
name: str = central.instance_name
super().__init__(central, unique_id, name)
self.hub_entities: dict[str, HmSystemVariable] = {}
Expand Down Expand Up @@ -252,7 +263,12 @@ class HmDummyHub(BaseHubEntity):

def __init__(self, central: hm_central.CentralUnit, use_entities: bool = False):
"""Initialize HomeMatic hub."""
unique_id: str = generate_unique_id(central.instance_name, prefix="hub")
unique_id: str = generate_unique_id(
domain=central.domain,
instance_name=central.instance_name,
address=central.instance_name,
prefix="hub",
)
name: str = central.instance_name
super().__init__(central, unique_id, name)
self.hub_entities: dict[str, BaseHubEntity] = {}
Expand Down
4 changes: 2 additions & 2 deletions hahomematic/json_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
self._session_id: str | None = None
self._username: str = self._central_config.username
self._password: str | None = self._central_config.password
self._json_tls: bool = self._central_config.json_tls
self._tls: bool = self._central_config.tls
self._tls_context: ssl.SSLContext = get_tls_context(
self._central_config.verify_tls
)
Expand Down Expand Up @@ -175,7 +175,7 @@ async def _post(
}

_LOGGER.debug("json_rpc_client._post: API-Endpoint: %s", self._url)
if self._json_tls:
if self._tls:
resp = await self._client_session.post(
self._url,
data=payload,
Expand Down
8 changes: 5 additions & 3 deletions hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import Any

import hahomematic.client as hm_client
from hahomematic.config import DEVICE_DESCRIPTIONS_DIR, PARAMSET_DESCRIPTIONS_DIR
from hahomematic.const import (
ATTR_HM_ADDRESS,
ATTR_HM_CHILDREN,
Expand All @@ -23,6 +22,8 @@
from hahomematic.helpers import check_or_create_directory

_LOGGER = logging.getLogger(__name__)
DEVICE_DESCRIPTIONS_DIR = "export_device_descriptions"
PARAMSET_DESCRIPTIONS_DIR = "export_paramset_descriptions"


class DeviceExporter:
Expand All @@ -33,6 +34,7 @@ def __init__(
):
self._client = client
self._central = client.central
self._domain = self._central.domain
self._interface_id = interface_id
self._device_address = device_address
self._random_id = "VCU%i" % random.randint(1000000, 9999999)
Expand Down Expand Up @@ -77,14 +79,14 @@ async def export_data(self) -> None:

# Save device_descriptions for device to file.
await self._save(
file_dir=DEVICE_DESCRIPTIONS_DIR,
file_dir=f"{self._domain}/{DEVICE_DESCRIPTIONS_DIR}",
filename=filename,
data=anonymize_device_descriptions,
)

# Save device_descriptions for device to file.
await self._save(
file_dir=PARAMSET_DESCRIPTIONS_DIR,
file_dir=f"{self._domain}/{PARAMSET_DESCRIPTIONS_DIR}",
filename=filename,
data=anonymize_paramset_descriptions,
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def readme():
},
PACKAGE_NAME = "hahomematic"
HERE = os.path.abspath(os.path.dirname(__file__))
VERSION = "0.12.0"
VERSION = "0.13.0"

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

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def systemcallback(src, *args):
GOT_DEVICES = True

central_unit = await CentralConfig(
domain="hahm",
name="ccu-dev",
loop=loop,
xml_rpc_server=register_xml_rpc_server(),
Expand Down
Loading

0 comments on commit e8774e0

Please sign in to comment.