Skip to content

Commit

Permalink
feat: Add support for Update entity
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Jan 19, 2025
1 parent 45f45d9 commit 6181127
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Provides discovery & command support for:
| Device Tracker | device_tracker | | |
| Event | event | | |
| Fan | Fan | Turn On, Turn Off, Set Percentage, Set Preset Mode, Set Oscillating, Set Direction | |
| Image | image | | References send HA image |
| Image | image | | References master HA image |
| Input Boolean | switch | Turn On, Turn Off | |
| Input Button | button | Press | |
| Input Number | number | Set Value | |
Expand All @@ -34,6 +34,7 @@ Provides discovery & command support for:
| Select | select | Select Option | |
| Switch | switch | Turn On, Turn Off | |
| Text | text | Set Value | |
| Update | update | Install | Not able to install specific version or trigger backup |

## [Buy Me A Beer 🍻](https://buymeacoffee.com/rogtp)
I work on this integration because I like things to work well for myself and others. Whilst I have now made significant changes to the integration, it would not be as it stands today without the initial creation by @koying. Please don't feel you are obligated to donate, but of course it is appreciated.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mqtt_discoverystream_alt/classes/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def build_config(self, config, entity_info: EntityInfo):
)

async def async_publish_state(self, new_state, mybase):
"""Build the state for a light."""
"""Build the state for a fan"""
await super().async_publish_state(new_state, mybase)
if ATTR_PERCENTAGE in new_state.attributes:
percentage = new_state.attributes[ATTR_PERCENTAGE]
Expand Down
99 changes: 99 additions & 0 deletions custom_components/mqtt_discoverystream_alt/classes/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Update methods for MQTT Discovery Statestream."""

from homeassistant.components import mqtt
from homeassistant.components.mqtt.update import (
CONF_DISPLAY_PRECISION,
CONF_LATEST_VERSION_TEMPLATE,
CONF_LATEST_VERSION_TOPIC,
CONF_PAYLOAD_INSTALL,
CONF_RELEASE_SUMMARY,
CONF_RELEASE_URL,
CONF_TITLE,
)
from homeassistant.components.update.const import (
ATTR_DISPLAY_PRECISION,
ATTR_INSTALLED_VERSION,
ATTR_LATEST_VERSION,
ATTR_RELEASE_SUMMARY,
ATTR_RELEASE_URL,
ATTR_TITLE,
SERVICE_INSTALL,
UpdateEntityFeature,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_STATE,
ATTR_SUPPORTED_FEATURES,
Platform,
)

from ..const import ATTR_ATTRIBUTES, ATTR_INSTALL, ATTR_INSTALL_COMMAND, CONF_CMD_T
from ..utils import (
EntityInfo,
add_config_command,
build_topic,
simple_attribute_add,
validate_message,
)
from .base_entity import DiscoveryEntity


class DiscoveryItem(DiscoveryEntity):
"""Update class."""

PLATFORM = Platform.UPDATE
PUBLISH_STATE = False

def build_config(self, config, entity_info: EntityInfo):
"""Build the config for a update."""

if (
entity_info.attributes[ATTR_SUPPORTED_FEATURES]
& UpdateEntityFeature.INSTALL
):
add_config_command(config, entity_info, CONF_CMD_T, ATTR_INSTALL)
config[CONF_PAYLOAD_INSTALL] = ATTR_INSTALL_COMMAND
config[CONF_LATEST_VERSION_TOPIC] = build_topic(ATTR_ATTRIBUTES)
config[CONF_LATEST_VERSION_TEMPLATE] = (
"{{ value_json['" + ATTR_LATEST_VERSION + "'] }}"
)

attributes = entity_info.attributes
if attributes.get(ATTR_RELEASE_URL):
config[CONF_RELEASE_URL] = attributes[ATTR_RELEASE_URL]
if attributes.get(ATTR_RELEASE_SUMMARY):
config[CONF_RELEASE_SUMMARY] = attributes[ATTR_RELEASE_SUMMARY]
if attributes.get(ATTR_TITLE):
config[CONF_TITLE] = attributes[ATTR_TITLE]
simple_attribute_add(
config, attributes, CONF_DISPLAY_PRECISION, ATTR_DISPLAY_PRECISION
)

async def async_publish_state(self, new_state, mybase):
"""Build the state for a update."""
await super().async_publish_state(new_state, mybase)
attributes = new_state.attributes
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
attributes[ATTR_INSTALLED_VERSION],
1,
self._publish_retain,
)

async def _async_handle_message(self, msg):
"""Handle a message for a fan."""
valid, domain, entity, command = validate_message(
self._hass, msg, DiscoveryItem.PLATFORM
)
if not valid:
return

entity_id = f"{domain}.{entity}"
service_payload = {
ATTR_ENTITY_ID: entity_id,
}
if command == ATTR_INSTALL_COMMAND:
await self._hass.services.async_call(
domain, SERVICE_INSTALL, service_payload
)
3 changes: 3 additions & 0 deletions custom_components/mqtt_discoverystream_alt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
ATTR_COLOR = "color"
ATTR_CONFIG = "config"
ATTR_DIRECTION_COMMAND = "command_direction"
ATTR_INSTALL = "install"
ATTR_INSTALL_COMMAND = "install"
ATTR_JSON = "JSON"
ATTR_MODE = "mode"
ATTR_MODE_COMMAND = "command_mode"
Expand Down Expand Up @@ -118,6 +120,7 @@
Platform.SENSOR: [],
Platform.SWITCH: [ATTR_SET],
Platform.TEXT: [ATTR_SET],
Platform.UPDATE: [ATTR_INSTALL_COMMAND],
INPUT_BOOLEAN_DOMAIN: [ATTR_SET],
INPUT_BUTTON_DOMAIN: [ATTR_SET],
INPUT_NUMBER_DOMAIN: [ATTR_SET],
Expand Down
4 changes: 2 additions & 2 deletions custom_components/mqtt_discoverystream_alt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def translate_entity_type(entity_id):
return f"{output_entity}.{ent_parts[1]}"


def simple_attribute_add(config, attributes, attribute_name, conf_name):
def simple_attribute_add(config, attributes, attribute_name, conf_name=None):
"""Simple check for attribute existence and inclusion."""
if attribute_name in attributes:
config[conf_name] = attributes[attribute_name]
config[conf_name or attribute_name] = attributes[attribute_name]


def simple_entry_attribute(config_device, attribute, conf_name):
Expand Down

0 comments on commit 6181127

Please sign in to comment.