Skip to content

Commit

Permalink
maint: Code tidy-up
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Jan 21, 2025
1 parent 19f7958 commit d87e8c6
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 159 deletions.
29 changes: 11 additions & 18 deletions custom_components/mqtt_discoverystream_alt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,20 @@ async def _state_publisher(evt: Event[EventStateChangedData]) -> None:
await publisher.async_state_publish(entity_id, new_state, mybase)
else:
payload = new_state.state
await mqtt.async_publish(hass, f"{mybase}state", payload, 1, publish_retain)
await _async_mqtt_publish(f"{mybase}state", payload)

if publish_timestamps:
if new_state.last_updated:
await mqtt.async_publish(
hass,
f"{mybase}last_updated",
new_state.last_updated.isoformat(),
1,
publish_retain,
await _async_mqtt_publish(
f"{mybase}last_updated", new_state.last_updated.isoformat()
)
if new_state.last_changed:
await mqtt.async_publish(
hass,
f"{mybase}last_changed",
new_state.last_changed.isoformat(),
1,
publish_retain,
await _async_mqtt_publish(
f"{mybase}last_changed", new_state.last_changed.isoformat()
)

if publish_attributes:
for key, val in new_state.attributes.items():
encoded_val = json.dumps(val, cls=JSONEncoder)
await mqtt.async_publish(
hass, mybase + key, encoded_val, 1, publish_retain
)
await _async_mqtt_publish(f"{mybase}{key}", val, encoded=True)

@callback
def _ha_started(hass: HomeAssistant) -> None:
Expand All @@ -108,6 +96,11 @@ def _ha_stopping(_: Event) -> None:

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _ha_stopping)

async def _async_mqtt_publish(mybase, value, encoded=False):
if encoded:
value = json.dumps(value, cls=JSONEncoder)
await mqtt.async_publish(hass, mybase, value, 1, publish_retain)

async_at_start(hass, _ha_started)

return True
36 changes: 25 additions & 11 deletions custom_components/mqtt_discoverystream_alt/classes/base_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async def async_publish_state(self, new_state, mybase):
mybase,
)

async def async_subscribe(self, command_topic):
"""Subscribe to messages for a cover."""
async def async_subscribe_commands(self, command_topic):
"""Subscribe to messages for an entity."""
if not self._commands:
return

Expand All @@ -62,16 +62,30 @@ async def _async_handle_message(self, msg):
async def _async_publish_base_attributes(self, new_state, mybase):
"""Publish the basic attributes for the entity state."""
if self._publish_state:
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
new_state.state,
1,
self._publish_retain,
)
await self._async_mqtt_publish(ATTR_STATE, new_state.state, mybase)

attributes = dict(new_state.attributes.items())
encoded = json.dumps(attributes, cls=JSONEncoder)
await self._async_mqtt_publish(
ATTR_ATTRIBUTES, attributes, mybase, encoded=True
)

async def _async_mqtt_publish(self, topic, value, mybase, encoded=False):
if encoded:
value = json.dumps(value, cls=JSONEncoder)
await mqtt.async_publish(
self._hass, f"{mybase}{ATTR_ATTRIBUTES}", encoded, 1, self._publish_retain
self._hass,
f"{mybase}{topic}",
value,
1,
self._publish_retain,
)

async def async_publish_attribute_if_exists(
self, new_state, mybase, attribute_name, strip=False
):
"""Publish a specific attribute"""
if attribute_name in new_state.attributes:
value = new_state.attributes[attribute_name]
if value and strip:
value = value.strip('"')
await self._async_mqtt_publish(attribute_name, value, mybase)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging

from homeassistant.components import mqtt
from homeassistant.components.button.const import SERVICE_PRESS
from homeassistant.components.number import (
ATTR_MAX,
Expand Down Expand Up @@ -167,13 +166,7 @@ async def async_publish_state(self, new_state, mybase):
"""Publish the state for a text."""

if new_state.state != STATE_UNKNOWN:
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
new_state.state,
1,
self._publish_retain,
)
await self._async_mqtt_publish(ATTR_STATE, new_state.state, mybase)

await super().async_publish_state(new_state, mybase)

Expand Down
24 changes: 10 additions & 14 deletions custom_components/mqtt_discoverystream_alt/classes/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging

from homeassistant.components import mqtt
from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE,
ATTR_HVAC_ACTION,
Expand Down Expand Up @@ -50,7 +49,6 @@
from ..utils import (
EntityInfo,
add_config_command,
async_publish_attribute,
build_topic,
validate_message,
)
Expand Down Expand Up @@ -100,31 +98,29 @@ def build_config(self, config, entity_info: EntityInfo):

async def async_publish_state(self, new_state, mybase):
"""Publish the state for a climate."""
await async_publish_attribute(
self._hass, new_state, mybase, ATTR_HVAC_ACTION, self._publish_retain
await self.async_publish_attribute_if_exists(
new_state,
mybase,
ATTR_HVAC_ACTION,
)
await async_publish_attribute(
self._hass,
await self.async_publish_attribute_if_exists(
new_state,
mybase,
ATTR_CURRENT_TEMPERATURE,
self._publish_retain,
)
await async_publish_attribute(
self._hass, new_state, mybase, ATTR_PRESET_MODE, self._publish_retain
await self.async_publish_attribute_if_exists(
new_state, mybase, ATTR_PRESET_MODE
)
await async_publish_attribute(
self._hass, new_state, mybase, ATTR_TEMPERATURE, self._publish_retain
await self.async_publish_attribute_if_exists(
new_state, mybase, ATTR_TEMPERATURE
)

await super().async_publish_state(new_state, mybase)

payload = new_state.state
if payload == STATE_UNAVAILABLE:
payload = STATE_OFF
await mqtt.async_publish(
self._hass, f"{mybase}{ATTR_HVAC_MODE}", payload, 1, self._publish_retain
)
await self._async_mqtt_publish(ATTR_HVAC_MODE, payload, mybase)

async def _async_handle_message(self, msg):
"""Handle a message for a switch."""
Expand Down
15 changes: 2 additions & 13 deletions custom_components/mqtt_discoverystream_alt/classes/event.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""event methods for MQTT Discovery Statestream."""

import json
import logging

from homeassistant.components import mqtt
from homeassistant.components.event import ATTR_EVENT_TYPE, ATTR_EVENT_TYPES
from homeassistant.const import ATTR_STATE, Platform
from homeassistant.helpers.json import JSONEncoder

from ..const import (
CONF_EVT_TYP,
Expand All @@ -30,15 +27,7 @@ def build_config(self, config, entity_info: EntityInfo):
async def async_publish_state(self, new_state, mybase):
"""Publish the state for a text."""

payload = json.dumps(
{ATTR_EVENT_TYPE: new_state.attributes[ATTR_EVENT_TYPE]}, cls=JSONEncoder
)
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
payload,
1,
self._publish_retain,
)
payload = {ATTR_EVENT_TYPE: new_state.attributes[ATTR_EVENT_TYPE]}
await self._async_mqtt_publish(ATTR_STATE, payload, mybase, encoded=True)

await super().async_publish_state(new_state, mybase)
9 changes: 1 addition & 8 deletions custom_components/mqtt_discoverystream_alt/classes/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging

from homeassistant.components import mqtt
from homeassistant.components.fan import (
ATTR_DIRECTION,
ATTR_OSCILLATING,
Expand Down Expand Up @@ -155,13 +154,7 @@ async def async_publish_state(self, new_state, mybase):
new_state.attributes[ATTR_PERCENTAGE]
/ new_state.attributes[ATTR_PERCENTAGE_STEP]
)
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_PERCENTAGE}",
int(percentage),
1,
self._publish_retain,
)
await self._async_mqtt_publish(ATTR_PERCENTAGE, int(percentage), mybase)

def _add_attribute(self, payload, new_state, attribute):
if attribute in new_state.attributes and new_state.attributes[attribute]:
Expand Down
27 changes: 6 additions & 21 deletions custom_components/mqtt_discoverystream_alt/classes/image.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""Image methods for MQTT Discovery Statestream."""

import json
import logging

from homeassistant.components import mqtt
from homeassistant.components.mqtt.image import CONF_URL_TOPIC
from homeassistant.const import ATTR_ENTITY_PICTURE, ATTR_STATE, Platform
from homeassistant.helpers.json import JSONEncoder
from homeassistant.helpers.network import get_url

from ..const import ATTR_ATTRIBUTES, CONF_ENT_PIC
Expand All @@ -30,31 +27,19 @@ def build_config(self, config, entity_info: EntityInfo):

async def async_publish_state(self, new_state, mybase):
"""Publish the state for a image."""
if self._publish_state:
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
new_state.state,
1,
self._publish_retain,
)

await self._async_mqtt_publish(ATTR_STATE, new_state.state, mybase)

attributes = dict(new_state.attributes.items())
if ATTR_ENTITY_PICTURE in attributes:
picture = attributes[ATTR_ENTITY_PICTURE]
if picture.startswith(f"/api/image_proxy/{new_state.entity_id}"):
url = get_url(self._hass, prefer_external=True)
picture = url + picture
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_ENTITY_PICTURE}",
picture,
1,
self._publish_retain,
)
await self._async_mqtt_publish(ATTR_ENTITY_PICTURE, picture, mybase)
del attributes[ATTR_ENTITY_PICTURE]
del attributes["access_token"]
encoded = json.dumps(attributes, cls=JSONEncoder)
await mqtt.async_publish(
self._hass, f"{mybase}{ATTR_ATTRIBUTES}", encoded, 1, self._publish_retain

await self._async_mqtt_publish(
ATTR_ATTRIBUTES, attributes, mybase, encoded=True
)
7 changes: 1 addition & 6 deletions custom_components/mqtt_discoverystream_alt/classes/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging

from homeassistant.components import mqtt
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_MODE,
Expand Down Expand Up @@ -32,7 +31,6 @@
Platform,
)
from homeassistant.helpers.entity import get_supported_features
from homeassistant.helpers.json import JSONEncoder

from ..const import (
ATTR_B,
Expand Down Expand Up @@ -102,10 +100,7 @@ async def async_publish_state(self, new_state, mybase):
if color := self._add_colors(new_state):
payload[ATTR_COLOR] = color

payload = json.dumps(payload, cls=JSONEncoder)
await mqtt.async_publish(
self._hass, f"{mybase}{ATTR_STATE}", payload, 1, self._publish_retain
)
await self._async_mqtt_publish(ATTR_STATE, payload, mybase, encoded=True)

def _add_attribute(self, payload, new_state, attribute):
if attribute in new_state.attributes and new_state.attributes[attribute]:
Expand Down
13 changes: 1 addition & 12 deletions custom_components/mqtt_discoverystream_alt/classes/update.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Update methods for MQTT Discovery Statestream."""

import json

from homeassistant.components import mqtt
from homeassistant.components.mqtt.update import (
CONF_DISPLAY_PRECISION,
CONF_LATEST_VERSION_TEMPLATE,
Expand Down Expand Up @@ -30,7 +27,6 @@
CONF_VALUE_TEMPLATE,
Platform,
)
from homeassistant.helpers.json import JSONEncoder

from ..const import ATTR_INSTALL, COMMAND_INSTALL, CONF_CMD_T
from ..utils import (
Expand Down Expand Up @@ -88,14 +84,7 @@ async def async_publish_state(self, new_state, mybase):
simple_attribute_add(state, attributes, ATTR_RELEASE_SUMMARY)
simple_attribute_add(state, attributes, ATTR_RELEASE_URL)
simple_attribute_add(state, attributes, ATTR_ENTITY_PICTURE)
encoded = json.dumps(state, cls=JSONEncoder)
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
encoded,
1,
self._publish_retain,
)
await self._async_mqtt_publish(ATTR_STATE, state, mybase, encoded=True)

async def _async_handle_message(self, msg):
"""Handle a message for a fan."""
Expand Down
11 changes: 1 addition & 10 deletions custom_components/mqtt_discoverystream_alt/classes/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging

from homeassistant.components import mqtt
from homeassistant.components.mqtt.vacuum import (
CONF_FAN_SPEED_LIST,
CONF_SEND_COMMAND_TOPIC,
Expand All @@ -28,7 +27,6 @@
ATTR_SUPPORTED_FEATURES,
Platform,
)
from homeassistant.helpers.json import JSONEncoder

from ..const import (
COMMAND_SEND,
Expand Down Expand Up @@ -92,14 +90,7 @@ async def async_publish_state(self, new_state, mybase):
state = {ATTR_STATE: new_state.state}
simple_attribute_add(state, attributes, ATTR_BATTERY_LEVEL)
simple_attribute_add(state, attributes, ATTR_FAN_SPEED)
encoded = json.dumps(state, cls=JSONEncoder)
await mqtt.async_publish(
self._hass,
f"{mybase}{ATTR_STATE}",
encoded,
1,
self._publish_retain,
)
await self._async_mqtt_publish(ATTR_STATE, state, mybase, encoded=True)

def _build_supported_features(self, feat_list):
sup_feat = []
Expand Down
4 changes: 3 additions & 1 deletion custom_components/mqtt_discoverystream_alt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ async def async_discovery_publish(self, entity_id, attributes, mybase):
)
entityclass = self.discovery_classes[ent_domain]
if ent_domain not in self._subscribed and self.subscribe_possible:
await entityclass.async_subscribe(set_topic(self._conf, CONF_COMMAND_TOPIC))
await entityclass.async_subscribe_commands(
set_topic(self._conf, CONF_COMMAND_TOPIC)
)
self._subscribed.append(ent_domain)

entityclass.build_config(config, entity_info)
Expand Down
Loading

0 comments on commit d87e8c6

Please sign in to comment.