Skip to content

Commit

Permalink
Add 'topic_prefix' configuration option. Fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
megakid committed Apr 19, 2023
1 parent af03465 commit 1f44999
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
5 changes: 2 additions & 3 deletions custom_components/hildebrand_glow_ihd_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
)
from homeassistant.core import HomeAssistant

from .const import(
DOMAIN,
)
from .const import DOMAIN, CONF_TOPIC_PREFIX

_LOGGER = logging.getLogger(__name__)

Expand All @@ -30,6 +28,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][entry.entry_id] = {}

hass.data[DOMAIN][entry.entry_id][CONF_DEVICE_ID] = entry.data[CONF_DEVICE_ID].strip().upper().replace(":", "").replace(" ", "")
hass.data[DOMAIN][entry.entry_id][CONF_TOPIC_PREFIX] = entry.data.get(CONF_TOPIC_PREFIX, "glow").strip().replace("#", "").replace(" ", "")

for component in PLATFORMS:
hass.async_create_task(
Expand Down
20 changes: 15 additions & 5 deletions custom_components/hildebrand_glow_ihd_mqtt/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.const import CONF_DEVICE_ID
from homeassistant.core import callback

from .const import DOMAIN
from .const import DOMAIN, CONF_TOPIC_PREFIX

_LOGGER = logging.getLogger(__name__)

Expand All @@ -20,17 +20,24 @@ async def async_step_user(self, user_input=None):
errors = {}
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=vol.Schema({vol.Required(CONF_DEVICE_ID, default='+'):str}), errors=errors
step_id="user", data_schema=vol.Schema({
vol.Required(CONF_DEVICE_ID, default='+'):str,
vol.Required(CONF_TOPIC_PREFIX, default='glow'):str
}), errors=errors
)

device_id = user_input[CONF_DEVICE_ID]
topic_prefix = user_input[CONF_TOPIC_PREFIX]

await self.async_set_unique_id('{}_{}'.format(DOMAIN, device_id))
self._abort_if_unique_id_configured()

return self.async_create_entry(
title="",
data={CONF_DEVICE_ID: device_id})
data={
CONF_DEVICE_ID: device_id,
CONF_TOPIC_PREFIX: topic_prefix
})


@staticmethod
Expand All @@ -52,5 +59,8 @@ async def async_step_init(self, user_input=None):
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

data_schema=vol.Schema({vol.Required(CONF_DEVICE_ID, default=self.config_entry.options.get(CONF_DEVICE_ID, "+")):str})
return self.async_show_form(step_id="user", data_schema=data_schema)
data_schema=vol.Schema({
vol.Required(CONF_DEVICE_ID, default=self.config_entry.options.get(CONF_DEVICE_ID, "+")):str,
vol.Required(CONF_TOPIC_PREFIX, default=self.config_entry.options.get(CONF_TOPIC_PREFIX, "glow")):str
})
return self.async_show_form(step_id="init", data_schema=data_schema)
1 change: 1 addition & 0 deletions custom_components/hildebrand_glow_ihd_mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
ATTR_ERROR = "error"
ATTR_STATE = "state"

CONF_TOPIC_PREFIX = "topic_prefix"
6 changes: 3 additions & 3 deletions custom_components/hildebrand_glow_ihd_mqtt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
SensorStateClass,
)
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from .const import DOMAIN
from .const import DOMAIN, CONF_TOPIC_PREFIX
from homeassistant.const import (
CONF_DEVICE_ID,
ATTR_DEVICE_ID,
Expand All @@ -23,7 +23,6 @@
VOLUME_CUBIC_METERS,
POWER_KILO_WATT,
SIGNAL_STRENGTH_DECIBELS,
PERCENTAGE,
)
from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -264,6 +263,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

# the config is defaulted to + which happens to mean we will subscribe to all devices
device_mac = hass.data[DOMAIN][config_entry.entry_id][CONF_DEVICE_ID]
topic_prefix = hass.data[DOMAIN][config_entry.entry_id][CONF_TOPIC_PREFIX] or "glow"

deviceUpdateGroups = {}

Expand All @@ -280,7 +280,7 @@ async def mqtt_message_received(message: ReceiveMessage):
for updateGroup in updateGroups:
updateGroup.process_update(message)

data_topic = "glow/#"
data_topic = f"{topic_prefix}/#"

await mqtt.async_subscribe(
hass, data_topic, mqtt_message_received, 1
Expand Down
8 changes: 5 additions & 3 deletions custom_components/hildebrand_glow_ihd_mqtt/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
"user": {
"description": "Configuring Hildebrand Glow IHD Local MQTT.",
"data": {
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)"
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)",
"topic_prefix": "Topic Prefix (leaving as 'glow' is usually the right thing to do!)"
},
"title": "Hildebrand Glow IHD Local MQTT"
}
}
},
"options": {
"step": {
"user": {
"init": {
"description": "Configuring Hildebrand Glow IHD Local MQTT.",
"data": {
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)"
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)",
"topic_prefix": "Topic Prefix (leaving as 'glow' is usually the right thing to do!)"
},
"title": "Hildebrand Glow IHD Local MQTT"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
"user": {
"description": "Configuring Hildebrand Glow IHD Local MQTT.",
"data": {
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)"
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)",
"topic_prefix": "Topic Prefix (leaving as 'glow' is usually the right thing to do!)"
},
"title": "Hildebrand Glow IHD Local MQTT"
}
}
},
"options": {
"step": {
"user": {
"init": {
"description": "Configuring Hildebrand Glow IHD Local MQTT.",
"data": {
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)"
"device_id": "Device Id (leave as + to auto-detect all devices on your MQTT)",
"topic_prefix": "Topic Prefix (leaving as 'glow' is usually the right thing to do!)"
},
"title": "Hildebrand Glow IHD Local MQTT"
}
Expand Down

0 comments on commit 1f44999

Please sign in to comment.