Skip to content

Commit

Permalink
Add mqtt topics to button plus device for text and update device on e…
Browse files Browse the repository at this point in the history
…nity change.
  • Loading branch information
koenhendriks committed Jan 4, 2024
1 parent ca5d935 commit d2b3628
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
3 changes: 1 addition & 2 deletions custom_components/button_plus/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ async def async_step_manual(self, user_input=None):

self.add_broker_to_config(device_config)
self.add_topics_to_buttons(device_config)
self.add_topics_to_labels(device_config)

await api_client.push_config(device_config)

Expand Down Expand Up @@ -257,7 +256,7 @@ def add_topics_to_buttons(self, device_config) -> DeviceConfiguration:
# Create topics for button top label
button.topics.append({
"brokerid": "ha-button-plus",
"topic": f"buttonplus/{device_id}/button/{button.button_id}/toplabel",
"topic": f"buttonplus/{device_id}/button/{button.button_id}/top_label",
"payload": "",
"eventtype": EventType.TOPLABEL
})
Expand Down
37 changes: 25 additions & 12 deletions custom_components/button_plus/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from __future__ import annotations

import logging
from typing import Any

from homeassistant.components.text import TextEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.components.mqtt import client as mqtt
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import ButtonPlusHub
Expand All @@ -15,25 +15,34 @@

_LOGGER = logging.getLogger(__name__)

texts = []
text_entities = []


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add switches for passed config_entry in HA."""
"""Add text entity for each top and main label from config_entry in HA."""

hub: ButtonPlusHub = hass.data[DOMAIN][config_entry.entry_id]

buttons = hub.config.mqtt_buttons

for button in buttons:
# _LOGGER.debug(f"Creating Texts with parameters: {button.button_id} {button.top_label} {button.label} {hub.hub_id}")
texts.append(ButtonPlusLabel(button.button_id, hub, button.label))
texts.append(ButtonPlusTopLabel(button.button_id, hub, button.top_label))
_LOGGER.debug(
f"Creating Texts with parameters: {button.button_id} {button.top_label} {button.label} {hub.hub_id}")

label_entity = ButtonPlusLabel(button.button_id, hub, button.label)
top_label_entity = ButtonPlusTopLabel(button.button_id, hub, button.top_label)

text_entities.append(label_entity)
text_entities.append(top_label_entity)

hub.add_label(button.button_id, label_entity)
hub.add_top_label(button.button_id, top_label_entity)

async_add_entities(texts)
async_add_entities(text_entities)


class ButtonPlusText(TextEntity):
Expand All @@ -47,6 +56,10 @@ def __init__(self, btn_id: int, hub: ButtonPlusHub, btn_label: str, text_type: s
self._attr_name = f'text-{text_type}-{btn_id}'
self._attr_native_value = btn_label

@property
def should_poll(self) -> bool:
return False

def update(self) -> None:
"""Fetch new state data for this label."""
# get latest stats from mqtt for this label
Expand Down Expand Up @@ -84,12 +97,12 @@ def device_info(self):

return device_info

def set_value(self, value: str) -> None:
"""Set the text value."""
self._attr_native_value = value

async def async_set_value(self, value: str) -> None:
"""Set the text value from mqtt."""
"""Set the text value and publish to mqtt."""
label_topic = f"buttonplus/{self._hub_id}/button/{self._btn_id}/{self._text_type}"
_LOGGER.debug(f"ButtonPlus label update for {self.entity_id}")
_LOGGER.debug(f"ButtonPlus label update to {label_topic} with new value: {value}")
await mqtt.async_publish(hass=self.hass, topic=label_topic, payload=value, qos=0)
self._attr_native_value = value


Expand Down

0 comments on commit d2b3628

Please sign in to comment.