Skip to content

Commit

Permalink
Merge pull request #389 from tekand/feature-prefix-device-name
Browse files Browse the repository at this point in the history
Add the possibility to prefix entites with device name
  • Loading branch information
yozik04 authored Nov 11, 2023
2 parents 05e3885 + fadb41b commit 1ef7958
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
1 change: 1 addition & 0 deletions config/pai.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import logging
# MQTT_CONTROL_TOPIC = 'control' # Base for control of other elements (ROOT/CONTROL/TYPE)
# MQTT_DEFINITIONS_TOPIC = 'control' # Base for definitions
# MQTT_HOMEASSISTANT_DISCOVERY_PREFIX = 'homeassistant'
# MQTT_HOMEASSISTANT_ENTITY_PREFIX = '' # If you want to prefix all entities you can use: "Paradox {serial_number} ", placeholders "serial_number" and "model" are supported. Default: "" - no prefix
# MQTT_OUTPUT_TOPIC = 'outputs'
# MQTT_DOOR_TOPIC = 'doors'
# MQTT_KEYPAD_TOPIC = 'keypads'
Expand Down
44 changes: 24 additions & 20 deletions paradox/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import os
import sys
import re
import sys


class Config(object):
class Config:
DEFAULTS = {
"LOGGING_LEVEL_CONSOLE": logging.INFO, # See documentation of Logging package
"LOGGING_LEVEL_FILE": logging.ERROR,
Expand Down Expand Up @@ -69,10 +69,10 @@ class Config(object):
), # Duration of a PGM pulse in seconds
"SYNC_TIME": False, # Update panel time periodically when time drifts more than SYNC_TIME_MIN_DRIFT
"SYNC_TIME_MIN_DRIFT": (
120,
int,
(120, 0xFFFFFFFF)
), # Minimum time drift in seconds to initiate time sync
120,
int,
(120, 0xFFFFFFFF),
), # Minimum time drift in seconds to initiate time sync
"SYNC_TIME_TIMEZONE": "", # By default pai uses the same timezone as pai host
"PASSWORD": (
None,
Expand Down Expand Up @@ -101,7 +101,11 @@ class Config(object):
"MQTT_USERNAME": (None, [str, type(None)]), # MQTT Username for authentication
"MQTT_PASSWORD": (None, [str, type(None)]), # MQTT Password
"MQTT_RETAIN": True, # Publish messages with Retain
"MQTT_QOS": (0, int, (0, 2)), # Publish messages with QOS level (0 - fire and forget, 1 - at least once, 2 - exactly once)
"MQTT_QOS": (
0,
int,
(0, 2),
), # Publish messages with QOS level (0 - fire and forget, 1 - at least once, 2 - exactly once)
"MQTT_PROTOCOL": ("3.1.1", str, ("3.1", "3.1.1", "5")), # Protocol to use
"MQTT_TRANSPORT": ("tcp", str, ("tcp", "websockets")), # Transport to use
"MQTT_BIND_ADDRESS": "", # MQTT Bind address (Paho default)
Expand All @@ -113,6 +117,7 @@ class Config(object):
), # Interval for republishing all data
"MQTT_HOMEASSISTANT_AUTODISCOVERY_ENABLE": False,
"MQTT_HOMEASSISTANT_CODE": (None, [str, type(None)]),
"MQTT_HOMEASSISTANT_ENTITY_PREFIX": "", # If you want to prefix all entities you can use: "Paradox {serial_number} ", placeholders "serial_number" and "model" are supported. Default: "" - no prefix
# MQTT Topics
"MQTT_BASE_TOPIC": "paradox", # Root of all topics
"MQTT_ZONE_TOPIC": "zones", # Base for zone states
Expand All @@ -136,6 +141,7 @@ class Config(object):
"MQTT_SEND_PANIC_TOPIC": "panic",
"MQTT_PUBLISH_RAW_EVENTS": True,
"MQTT_PUBLISH_DEFINITIONS": False, # Publish definitions of partitions/zones/users to mqtt.
"MQTT_PREFIX_DEVICE_NAME": False, # Add device ID as prefix to entity names: Paradox 12345678
"MQTT_INTERFACE_TOPIC": "interface",
"MQTT_TOGGLE_CODES": {},
"MQTT_USE_NUMERIC_STATES": False, # use 0 and 1 instead of True and False
Expand Down Expand Up @@ -172,12 +178,12 @@ class Config(object):
["DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"],
),
"HOMEASSISTANT_PUBLISH_PARTITION_PROPERTIES": [ # List of partition properties to publish
'target_state',
'current_state'
"target_state",
"current_state",
],
"HOMEASSISTANT_PUBLISH_ZONE_PROPERTIES": [ # List of zone properties to publish
'open',
'tamper'
"open",
"tamper",
],
"HOMEASSISTANT_NOTIFICATIONS_IGNORE_EVENTS": [], # List of tuples or regexp matching "type,label,property=value,property2=value" eg. [(major, minor), "zone:HOME:entry_delay=True", ...]
"HOMEASSISTANT_NOTIFICATIONS_ALLOW_EVENTS": [], # Same as before but as a white list. Default is use EVENT_FILTERS
Expand Down Expand Up @@ -280,11 +286,7 @@ class Config(object):
CONFIG_FILE_LOCATION = (None,)

def __dir__(self):
return (
list(self.DEFAULTS.keys())
+ list(self.__class__.__dict__)
+ dir(super(Config, self))
)
return list(self.DEFAULTS.keys()) + list(self.__class__.__dict__) + dir(super())

def __init__(self):
self._reset_defaults()
Expand Down Expand Up @@ -314,9 +316,9 @@ def load(self, alt_location=None):
else:
default_type = [type(default)]

if float in default_type and not int in default_type:
if float in default_type and int not in default_type:
default_type.append(int)
if int in default_type and not float in default_type:
if int in default_type and float not in default_type:
default_type.append(float)
if type(v) in default_type:
setattr(self, k, v)
Expand Down Expand Up @@ -425,8 +427,9 @@ def _reset_defaults(self):

config = Config()

comma_splitter_re = re.compile('\s*,\s*')
range_re = re.compile('(\d+)\s*-(\d+)\s*$')
comma_splitter_re = re.compile(r"\s*,\s*")
range_re = re.compile(r"(\d+)\s*-(\d+)\s*$")


def string_to_id_list(input: str):
arr = []
Expand All @@ -443,6 +446,7 @@ def string_to_id_list(input: str):

return arr


def get_limits_for_type(elem_type: str, default: list = None):
limits = config.LIMITS.get(elem_type, default)
if limits is None:
Expand Down
44 changes: 29 additions & 15 deletions paradox/interfaces/mqtt/entities/abstract_entity.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from paradox.config import config as cfg
from paradox.interfaces.mqtt.helpers import get_control_topic_prefix, get_state_topic_prefix
from paradox.interfaces.mqtt.entities.device import Device
from paradox.interfaces.mqtt.helpers import (
get_control_topic_prefix,
get_state_topic_prefix,
)


def to_label(txt):
return txt.replace('_', ' ').title()
return txt.replace("_", " ").title()


class AbstractEntity:
def __init__(self, device, availability_topic):
def __init__(self, device: Device, availability_topic):
self.availability_topic = availability_topic
self.device = device

Expand All @@ -28,20 +34,30 @@ def entity_name(self):

@property
def configuration_topic(self):
return "/".join([
cfg.MQTT_HOMEASSISTANT_DISCOVERY_PREFIX,
self.hass_entity_type,
self.device.serial_number,
self.entity_id,
"config"
])
return "/".join(
[
cfg.MQTT_HOMEASSISTANT_DISCOVERY_PREFIX,
self.hass_entity_type,
self.device.serial_number,
self.entity_id,
"config",
]
)

def serialize(self):
prefix = cfg.MQTT_HOMEASSISTANT_ENTITY_PREFIX.format(
{
"serial_number",
self.device.serial_number,
"model",
self.device.model,
}
)
return dict(
availability_topic=self.availability_topic,
device=self.device,
name=f'{self.entity_name}',
unique_id=f'paradox_{self.device.serial_number}_{self.entity_id}',
name=prefix + f"{self.entity_name}",
unique_id=f"paradox_{self.device.serial_number}_{self.entity_id}",
state_topic=self.state_topic,
)

Expand All @@ -59,7 +75,5 @@ def state_topic(self):
class AbstractControllableEntity(AbstractEntity):
def serialize(self):
config = super().serialize()
config.update(dict(
command_topic=self.command_topic
))
config.update(dict(command_topic=self.command_topic))
return config

0 comments on commit 1ef7958

Please sign in to comment.