Skip to content

Commit

Permalink
Return unique_id if name is not in cache (#144)
Browse files Browse the repository at this point in the history
* Remove VALVE_STATE from ED

* Return unique_id if name is not in cache

* Remove no longer needed press_virtual_remote_key

* Fix docstring

* Fix log messages
  • Loading branch information
SukramJ authored Jan 8, 2022
1 parent 03a6ee3 commit d5995c0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 75 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 0.16.0 (2022-01-08)
- Return unique_id if name is not in cache
- Remove no longer needed press_virtual_remote_key

Version 0.15.2 (2022-01-07)
- Add devices to CustomEntity
- HmIP-WGC
Expand Down
32 changes: 3 additions & 29 deletions hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ async def delete_devices(self, interface_id: str, addresses: list[str]) -> None:
async def add_new_devices(
self, interface_id: str, dev_descriptions: list[dict[str, Any]]
) -> None:
"""Async implementation"""
"""Add new devices to central unit."""
_LOGGER.debug(
"CentralUnit.add_new_devices: interface_id = %s, dev_descriptions = %s",
interface_id,
Expand All @@ -276,7 +276,7 @@ async def add_new_devices(

if interface_id not in self.clients:
_LOGGER.error(
"RPCFunctions.newDevices: Missing client for interface_id %s.",
"CentralUnit.add_new_devices: Missing client for interface_id %s.",
interface_id,
)
return None
Expand All @@ -293,7 +293,7 @@ async def add_new_devices(
self.raw_devices.add_device_description(interface_id, dev_desc)
await client.fetch_paramsets(dev_desc)
except Exception:
_LOGGER.exception("RPCFunctions.newDevices: Exception")
_LOGGER.exception("CentralUnit.add_new_devices: Exception")
await self.raw_devices.save()
await self.paramsets.save()
await client.fetch_names()
Expand Down Expand Up @@ -475,32 +475,6 @@ def _get_virtual_remote(self, device_address: str) -> HmDevice | None:
return virtual_remote
return None

async def press_virtual_remote_key(
self, channel_address: str, parameter: str
) -> None:
"""Simulate a key press on the virtual remote."""
if ":" not in channel_address:
_LOGGER.warning(
"CentralUnit.press_virtual_remote_key: channel_address is missing channel information."
)

if channel_address.startswith(HM_VIRTUAL_REMOTE_HM.upper()):
channel_address = channel_address.replace(
HM_VIRTUAL_REMOTE_HM.upper(), HM_VIRTUAL_REMOTE_HM
)
if channel_address.startswith(HM_VIRTUAL_REMOTE_HMIP.upper()):
channel_address = channel_address.replace(
HM_VIRTUAL_REMOTE_HMIP.upper(), HM_VIRTUAL_REMOTE_HMIP
)

if virtual_remote := self._get_virtual_remote(
get_device_address(channel_address)
):
if virtual_remote_channel := virtual_remote.action_events.get(
(channel_address, parameter)
):
await virtual_remote_channel.send_value(True)

def get_hm_entities_by_hmplatform(self, platform: HmPlatform) -> list[BaseEntity]:
"""
Return all hm-entities by platform
Expand Down
1 change: 0 additions & 1 deletion hahomematic/devices/entity_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ def __str__(self) -> str:
1: {
FIELD_HUMIDITY: "ACTUAL_HUMIDITY",
FIELD_TEMPERATURE: "ACTUAL_TEMPERATURE",
FIELD_LEVEL: "VALVE_STATE",
}
},
},
Expand Down
85 changes: 41 additions & 44 deletions hahomematic/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ def get_entity_name(
device_type: str,
) -> str:
"""generate name for entity"""
entity_name = _get_base_name_from_channel_or_device(
if entity_name := _get_base_name_from_channel_or_device(
central=central,
channel_address=channel_address,
unique_id=unique_id,
device_type=device_type,
)

if entity_name.count(":") == 1:
d_name = entity_name.split(":")[0]
p_name = parameter.title().replace("_", " ")
c_name = ""
if central.paramsets.has_multiple_channels(
channel_address=channel_address, parameter=parameter
):
c_no = entity_name.split(":")[1]
c_name = "" if c_no == "0" else f" ch{c_no}"
entity_name = f"{d_name} {p_name}{c_name}"
else:
d_name = entity_name
p_name = parameter.title().replace("_", " ")
entity_name = f"{d_name} {p_name}"
return entity_name
):
if entity_name.count(":") == 1:
d_name = entity_name.split(":")[0]
p_name = parameter.title().replace("_", " ")
c_name = ""
if central.paramsets.has_multiple_channels(
channel_address=channel_address, parameter=parameter
):
c_no = entity_name.split(":")[1]
c_name = "" if c_no == "0" else f" ch{c_no}"
entity_name = f"{d_name} {p_name}{c_name}"
else:
d_name = entity_name
p_name = parameter.title().replace("_", " ")
entity_name = f"{d_name} {p_name}"
return entity_name

return unique_id


def get_event_name(
Expand All @@ -151,23 +151,24 @@ def get_event_name(
device_type: str,
) -> str:
"""generate name for event"""
event_name = _get_base_name_from_channel_or_device(
if event_name := _get_base_name_from_channel_or_device(
central=central,
channel_address=channel_address,
unique_id=unique_id,
device_type=device_type,
)
if event_name.count(":") == 1:
d_name = event_name.split(":")[0]
p_name = parameter.title().replace("_", " ")
c_no = event_name.split(":")[1]
c_name = "" if c_no == "0" else f" Channel {c_no}"
event_name = f"{d_name}{c_name} {p_name}"
else:
d_name = event_name
p_name = parameter.title().replace("_", " ")
event_name = f"{d_name} {p_name}"
return event_name
):
if event_name.count(":") == 1:
d_name = event_name.split(":")[0]
p_name = parameter.title().replace("_", " ")
c_no = event_name.split(":")[1]
c_name = "" if c_no == "0" else f" Channel {c_no}"
event_name = f"{d_name}{c_name} {p_name}"
else:
d_name = event_name
p_name = parameter.title().replace("_", " ")
event_name = f"{d_name} {p_name}"
return event_name

return unique_id


def get_custom_entity_name(
Expand All @@ -179,16 +180,16 @@ def get_custom_entity_name(
is_only_primary_channel: bool,
) -> str:
"""Rename name for custom entity"""
custom_entity_name = _get_base_name_from_channel_or_device(
if custom_entity_name := _get_base_name_from_channel_or_device(
central=central,
channel_address=f"{device_address}:{channel_no}",
unique_id=unique_id,
device_type=device_type,
)
if is_only_primary_channel and ":" in custom_entity_name:
return custom_entity_name.split(":")[0]
):
if is_only_primary_channel and ":" in custom_entity_name:
return custom_entity_name.split(":")[0]
return custom_entity_name.replace(":", " ch")

return custom_entity_name.replace(":", " ch")
return unique_id


def check_channel_is_only_primary_channel(
Expand All @@ -205,19 +206,15 @@ def check_channel_is_only_primary_channel(
def _get_base_name_from_channel_or_device(
central: hm_central.CentralUnit,
channel_address: str,
unique_id: str,
device_type: str,
) -> str:
) -> str | None:
"""Get the name from channel if it's not default, otherwise from device."""
default_channel_name = f"{device_type} {channel_address}"
name = central.names.get_name(channel_address)
if name is None or name == default_channel_name:
channel_no = get_device_channel(channel_address)
if device_name := central.names.get_name(get_device_address(channel_address)):
name = f"{device_name}:{channel_no}"
if name is None:
name = unique_id

return name


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.15.2"
VERSION = "0.16.0"

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

Expand Down

0 comments on commit d5995c0

Please sign in to comment.