From 1f449993e1bfa856b7fb4807fd2bfba99b0808d1 Mon Sep 17 00:00:00 2001 From: James Connor Date: Wed, 19 Apr 2023 21:57:34 +0100 Subject: [PATCH] Add 'topic_prefix' configuration option. Fixes #24 --- .../hildebrand_glow_ihd_mqtt/__init__.py | 5 ++--- .../hildebrand_glow_ihd_mqtt/config_flow.py | 20 ++++++++++++++----- .../hildebrand_glow_ihd_mqtt/const.py | 1 + .../hildebrand_glow_ihd_mqtt/sensor.py | 6 +++--- .../hildebrand_glow_ihd_mqtt/strings.json | 8 +++++--- .../translations/en.json | 8 +++++--- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/custom_components/hildebrand_glow_ihd_mqtt/__init__.py b/custom_components/hildebrand_glow_ihd_mqtt/__init__.py index ad4baf8..532f01e 100755 --- a/custom_components/hildebrand_glow_ihd_mqtt/__init__.py +++ b/custom_components/hildebrand_glow_ihd_mqtt/__init__.py @@ -7,9 +7,7 @@ ) from homeassistant.core import HomeAssistant -from .const import( - DOMAIN, -) +from .const import DOMAIN, CONF_TOPIC_PREFIX _LOGGER = logging.getLogger(__name__) @@ -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( diff --git a/custom_components/hildebrand_glow_ihd_mqtt/config_flow.py b/custom_components/hildebrand_glow_ihd_mqtt/config_flow.py index 2139fb7..fa7e885 100755 --- a/custom_components/hildebrand_glow_ihd_mqtt/config_flow.py +++ b/custom_components/hildebrand_glow_ihd_mqtt/config_flow.py @@ -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__) @@ -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 @@ -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) \ No newline at end of file + 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) \ No newline at end of file diff --git a/custom_components/hildebrand_glow_ihd_mqtt/const.py b/custom_components/hildebrand_glow_ihd_mqtt/const.py index 0e20e3f..71670e4 100755 --- a/custom_components/hildebrand_glow_ihd_mqtt/const.py +++ b/custom_components/hildebrand_glow_ihd_mqtt/const.py @@ -14,3 +14,4 @@ ATTR_ERROR = "error" ATTR_STATE = "state" +CONF_TOPIC_PREFIX = "topic_prefix" \ No newline at end of file diff --git a/custom_components/hildebrand_glow_ihd_mqtt/sensor.py b/custom_components/hildebrand_glow_ihd_mqtt/sensor.py index f60283a..311803b 100755 --- a/custom_components/hildebrand_glow_ihd_mqtt/sensor.py +++ b/custom_components/hildebrand_glow_ihd_mqtt/sensor.py @@ -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, @@ -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 @@ -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 = {} @@ -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 diff --git a/custom_components/hildebrand_glow_ihd_mqtt/strings.json b/custom_components/hildebrand_glow_ihd_mqtt/strings.json index 30d4d20..b43edf2 100755 --- a/custom_components/hildebrand_glow_ihd_mqtt/strings.json +++ b/custom_components/hildebrand_glow_ihd_mqtt/strings.json @@ -13,7 +13,8 @@ "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" } @@ -21,10 +22,11 @@ }, "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" } diff --git a/custom_components/hildebrand_glow_ihd_mqtt/translations/en.json b/custom_components/hildebrand_glow_ihd_mqtt/translations/en.json index 6d2a9bc..0a3f2f8 100755 --- a/custom_components/hildebrand_glow_ihd_mqtt/translations/en.json +++ b/custom_components/hildebrand_glow_ihd_mqtt/translations/en.json @@ -13,7 +13,8 @@ "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" } @@ -21,10 +22,11 @@ }, "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" }