Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed May 13, 2024
1 parent ee7172a commit d36d97d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 87 deletions.
67 changes: 25 additions & 42 deletions custom_components/openhasp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pathlib
import re

from homeassistant.components.mqtt import async_subscribe, async_publish
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
Expand Down Expand Up @@ -391,8 +392,8 @@ async def page_update_received(msg):
_LOGGER.error("%s in %s", err, msg.payload)

self._subscriptions.append(
await self.hass.components.mqtt.async_subscribe(
f"{self._topic}/state/page", page_update_received
await async_subscribe(
self.hass, f"{self._topic}/state/page", page_update_received
)
)

Expand Down Expand Up @@ -433,11 +434,13 @@ async def statusupdate_message_received(msg):
_LOGGER.error("While processing status update: %s", err)

self._subscriptions.append(
await self.hass.components.mqtt.async_subscribe(
f"{self._topic}/state/statusupdate", statusupdate_message_received
await async_subscribe(
self.hass,
f"{self._topic}/state/statusupdate",
statusupdate_message_received,
)
)
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass, f"{self._topic}/command", "statusupdate", qos=0, retain=False
)

Expand All @@ -451,8 +454,8 @@ async def idle_message_received(msg):
_LOGGER.error("While processing idle message: %s", err)

self._subscriptions.append(
await self.hass.components.mqtt.async_subscribe(
f"{self._topic}/state/idle", idle_message_received
await async_subscribe(
self.hass, f"{self._topic}/state/idle", idle_message_received
)
)

Expand Down Expand Up @@ -491,9 +494,7 @@ async def lwt_message_received(msg):
_LOGGER.error("While processing LWT: %s", err)

self._subscriptions.append(
await self.hass.components.mqtt.async_subscribe(
f"{self._topic}/LWT", lwt_message_received
)
await async_subscribe(self.hass, f"{self._topic}/LWT", lwt_message_received)
)

@property
Expand All @@ -515,9 +516,7 @@ async def async_wakeup(self):
"""Wake up the display."""
cmd_topic = f"{self._topic}/command"
_LOGGER.warning("Wakeup will be deprecated in 0.8.0") # remove in version 0.8.0
await self.hass.components.mqtt.async_publish(
self.hass, cmd_topic, "wakeup", qos=0, retain=False
)
await async_publish(self.hass, cmd_topic, "wakeup", qos=0, retain=False)

async def async_change_page_next(self):
"""Change page to next one."""
Expand All @@ -526,9 +525,7 @@ async def async_change_page_next(self):
"page next service will be deprecated in 0.8.0"
) # remove in version 0.8.0

await self.hass.components.mqtt.async_publish(
self.hass, cmd_topic, "page next", qos=0, retain=False
)
await async_publish(self.hass, cmd_topic, "page next", qos=0, retain=False)

async def async_change_page_prev(self):
"""Change page to previous one."""
Expand All @@ -537,22 +534,18 @@ async def async_change_page_prev(self):
"page prev service will be deprecated in 0.8.0"
) # remove in version 0.8.0

await self.hass.components.mqtt.async_publish(
self.hass, cmd_topic, "page prev", qos=0, retain=False
)
await async_publish(self.hass, cmd_topic, "page prev", qos=0, retain=False)

async def async_clearpage(self, page="all"):
"""Clear page."""
cmd_topic = f"{self._topic}/command"

await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass, cmd_topic, f"clearpage {page}", qos=0, retain=False
)

if page == "all":
await self.hass.components.mqtt.async_publish(
self.hass, cmd_topic, "page 1", qos=0, retain=False
)
await async_publish(self.hass, cmd_topic, "page 1", qos=0, retain=False)

async def async_change_page(self, page):
"""Change page to number."""
Expand All @@ -570,14 +563,12 @@ async def async_change_page(self, page):
self._page = page

_LOGGER.debug("Change page %s", self._page)
await self.hass.components.mqtt.async_publish(
self.hass, cmd_topic, self._page, qos=0, retain=False
)
await async_publish(self.hass, cmd_topic, self._page, qos=0, retain=False)
self.async_write_ha_state()

async def async_command_service(self, keyword, parameters):
"""Send commands directly to the plate entity."""
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{self._topic}/command",
f"{keyword} {parameters}".strip(),
Expand All @@ -587,7 +578,7 @@ async def async_command_service(self, keyword, parameters):

async def async_config_service(self, submodule, parameters):
"""Send configuration commands to plate entity."""
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{self._topic}/config/{submodule}",
f"{parameters}".strip(),
Expand Down Expand Up @@ -616,9 +607,7 @@ async def async_push_image(

_LOGGER.debug("Push %s with %s", cmd_topic, rgb_image_url)

await self.hass.components.mqtt.async_publish(
self.hass, cmd_topic, rgb_image_url, qos=0, retain=False
)
await async_publish(self.hass, cmd_topic, rgb_image_url, qos=0, retain=False)

async def refresh(self):
"""Refresh objects in the SwitchPlate."""
Expand All @@ -642,7 +631,7 @@ async def send_lines(lines):
mqtt_payload_buffer = ""
for line in lines:
if len(mqtt_payload_buffer) + len(line) > 1000:
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{cmd_topic}/jsonl",
mqtt_payload_buffer,
Expand All @@ -652,7 +641,7 @@ async def send_lines(lines):
mqtt_payload_buffer = line
else:
mqtt_payload_buffer = mqtt_payload_buffer + line
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{cmd_topic}/jsonl",
mqtt_payload_buffer,
Expand Down Expand Up @@ -770,9 +759,7 @@ async def _async_template_result_changed(event, updates):
result,
)

await self.hass.components.mqtt.async_publish(
self.hass, self.command_topic + _property, result
)
await async_publish(self.hass, self.command_topic + _property, result)

property_template = async_track_template_result(
self.hass,
Expand All @@ -787,9 +774,7 @@ async def refresh(self):
"""Refresh based on cached values."""
for _property, result in self.cached_properties.items():
_LOGGER.debug("Refresh object %s.%s = %s", self.obj_id, _property, result)
await self.hass.components.mqtt.async_publish(
self.hass, self.command_topic + _property, result
)
await async_publish(self.hass, self.command_topic + _property, result)

async def async_listen_hasp_events(self):
"""Listen to messages on MQTT for HASP events."""
Expand Down Expand Up @@ -834,6 +819,4 @@ async def message_received(msg):
)

_LOGGER.debug("Subscribe to '%s' events on '%s'", self.obj_id, self.state_topic)
return await self.hass.components.mqtt.async_subscribe(
self.state_topic, message_received
)
return await async_subscribe(self.hass, self.state_topic, message_received)
12 changes: 8 additions & 4 deletions custom_components/openhasp/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import logging
from typing import Callable

from homeassistant.components.mqtt import async_publish, async_subscribe
from homeassistant.components.binary_sensor import BinarySensorEntity

# pylint: disable=R0801
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
Expand Down Expand Up @@ -68,7 +70,7 @@ def device_class(self):

async def refresh(self):
"""Force sync of plate state back to binary sensor."""
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{self._topic}/command/input{self._gpio}",
"",
Expand Down Expand Up @@ -96,12 +98,14 @@ async def state_message_received(msg):
_LOGGER.error(err)

self._subscriptions.append(
await self.hass.components.mqtt.async_subscribe(
f"{self._topic}/state/input{self._gpio}", state_message_received
await async_subscribe(
self.hass,
f"{self._topic}/state/input{self._gpio}",
state_message_received,
)
)

await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{self._topic}/command/input{self._gpio}",
"",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/openhasp/button.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Support for current page numbers."""
import logging

from homeassistant.components.mqtt import async_publish
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
Expand Down Expand Up @@ -45,7 +46,7 @@ def __init__(self, name, hwid, topic) -> None:

async def async_press(self) -> None:
"""Handle the button press."""
await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
f"{self._topic}/command/restart",
"",
Expand Down
22 changes: 14 additions & 8 deletions custom_components/openhasp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os

from homeassistant.components.mqtt import async_publish
from homeassistant import config_entries, data_entry_flow, exceptions
from homeassistant.components.mqtt import valid_subscribe_topic
from homeassistant.const import CONF_NAME
Expand Down Expand Up @@ -75,7 +76,7 @@ async def async_step_user(self, user_input=None):
"""Handle a flow initialized by User."""
_LOGGER.info("Discovery Only")

await self.hass.components.mqtt.async_publish(
await async_publish(
self.hass,
"hasp/broadcast/command/discovery",
"discovery",
Expand All @@ -92,13 +93,18 @@ async def async_step_zeroconf(self, discovery_info=None):
_discovered[CONF_TOPIC] = _discovered[DISCOVERED_NODE_T][:-1]

for key in [
DISCOVERED_PAGES,
DISCOVERED_POWER,
DISCOVERED_LIGHT,
DISCOVERED_DIM,
DISCOVERED_INPUT,
]:
_LOGGER.debug("[%s] Discovered %s = %s", _discovered[CONF_TOPIC], key, _discovered.get(key))
DISCOVERED_PAGES,
DISCOVERED_POWER,
DISCOVERED_LIGHT,
DISCOVERED_DIM,
DISCOVERED_INPUT,
]:
_LOGGER.debug(
"[%s] Discovered %s = %s",
_discovered[CONF_TOPIC],
key,
_discovered.get(key),
)
_discovered[key] = json.loads(_discovered.get(key))

return await self._process_discovery(_discovered)
Expand Down
Loading

0 comments on commit d36d97d

Please sign in to comment.