Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes issue #10: adapt to MQTT API changes #11

Merged
merged 3 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Hisense TV Integration for Home Assistant

Integration an Hisense TV as media player into Home Assistant. The communication is handled via the integrated MQTT broker and wake-on-LAN.
Requires Home Assistant >= `2021.12.x`.

## Current features:
* Turn on / off
Expand Down
1 change: 1 addition & 0 deletions custom_components/hisense_tv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ async def async_setup(hass, config):
_LOGGER.debug("async_setup")
hass.data.setdefault(DOMAIN, {})
return True

45 changes: 27 additions & 18 deletions custom_components/hisense_tv/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Hisense TV config flow."""
import json
from json.decoder import JSONDecodeError
import logging

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import mqtt
from homeassistant.const import CONF_MAC, CONF_NAME, CONF_PIN
from homeassistant.data_entry_flow import FlowResult

from .const import (
CONF_MQTT_IN,
Expand All @@ -15,7 +18,6 @@
DEFAULT_NAME,
DOMAIN,
)
from .helper import mqtt_pub_sub

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,8 +56,11 @@ async def _async_pin_not_needed(self, message):

async def _async_authcode_response(self, message):
self._unsubscribe()
payload = json.loads(message.payload)
_LOGGER.debug("_async_authcode_respone %s" % payload)
try:
payload = json.loads(message.payload)
except JSONDecodeError:
payload = {}
_LOGGER.debug("_async_authcode_respone %s", payload)
self.task_auth = payload.get("result") == 1
self.hass.async_create_task(
self.hass.config_entries.flow.async_configure(flow_id=self.flow_id)
Expand All @@ -69,15 +74,15 @@ def _unsubscribe(self):
self._unsubscribe_sourcelist()
self._unsubscribe_sourcelist = None

async def async_step_user(self, info):
async def async_step_user(self, user_input) -> FlowResult:
if self.task_auth is True:
return self.async_show_progress_done(next_step_id="finish")

if self.task_auth is False:
self.task_auth = None
return self.async_show_progress_done(next_step_id="auth")

if info is None:
if user_input is None:
_LOGGER.debug("async_step_user INFO None")
return self.async_show_form(
step_id="user",
Expand All @@ -90,21 +95,21 @@ async def async_step_user(self, info):
}
),
)
else:
_LOGGER.debug("async_step_user NOT task_mqtt")
self.task_mqtt = {
CONF_MAC: info.get(CONF_MAC),
CONF_NAME: info.get(CONF_NAME),
CONF_MQTT_IN: info.get(CONF_MQTT_IN),
CONF_MQTT_OUT: info.get(CONF_MQTT_OUT),
}

await self._check_authentication(client_id=DEFAULT_CLIENT_ID)
_LOGGER.debug("async_step_user NOT task_mqtt")
self.task_mqtt = {
CONF_MAC: user_input.get(CONF_MAC),
CONF_NAME: user_input.get(CONF_NAME),
CONF_MQTT_IN: user_input.get(CONF_MQTT_IN),
CONF_MQTT_OUT: user_input.get(CONF_MQTT_OUT),
}

return self.async_show_progress(
step_id="user",
progress_action="progress_action",
)
await self._check_authentication(client_id=DEFAULT_CLIENT_ID)

return self.async_show_progress(
step_id="user",
progress_action="progress_action",
)

async def _check_authentication(self, client_id):
self._unsubscribe_auth = await mqtt.async_subscribe(
Expand Down Expand Up @@ -133,10 +138,12 @@ async def _check_authentication(self, client_id):
)

async def async_step_reauth(self, user_input=None):
"""Reauth handler."""
self.task_auth = None
return await self.async_step_auth(user_input=user_input)

async def async_step_auth(self, user_input=None):
"""Auth handler."""
if self.task_auth is True:
_LOGGER.debug("async_step_auth finish")
return self.async_show_progress_done(next_step_id="finish")
Expand Down Expand Up @@ -178,10 +185,12 @@ async def async_step_auth(self, user_input=None):
)

async def async_step_finish(self, user_input=None):
"""Finish config flow."""
_LOGGER.debug("async_step_finish")
return self.async_create_entry(title=self._name, data=self.task_mqtt)

async def async_step_import(self, data):
"""Handle import from YAML."""
_LOGGER.debug("async_step_import")
return self.async_create_entry(title=data[CONF_NAME], data=data)

11 changes: 8 additions & 3 deletions custom_components/hisense_tv/helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Hisene TV integration helper methods."""
import asyncio
import logging

Expand All @@ -10,6 +11,7 @@


async def mqtt_pub_sub(hass, pub, sub, payload=""):
"""Wrapper for publishing MQTT topics and receive replies on a subscibed topic."""
loop = asyncio.get_event_loop()
queue = asyncio.Queue()

Expand All @@ -21,11 +23,13 @@ async def get():
yield await asyncio.wait_for(queue.get(), timeout=10)

unsubscribe = await mqtt.async_subscribe(hass=hass, topic=sub, msg_callback=put)
mqtt.async_publish(hass=hass, topic=pub, payload=payload)
await mqtt.async_publish(hass=hass, topic=pub, payload=payload)
return get(), unsubscribe


class HisenseTvBase(object):
"""Hisense TV base entity."""

def __init__(
self, hass, name: str, mqtt_in: str, mqtt_out: str, mac: str, uid: str
):
Expand Down Expand Up @@ -53,13 +57,14 @@ def _out_topic(self, topic=""):
out_topic = self._mqtt_out + topic % self._client
except:
out_topic = self._mqtt_out + topic % self._client
_LOGGER.debug("_out_topic: %s" % out_topic)
_LOGGER.debug("_out_topic: %s", out_topic)
return out_topic

def _in_topic(self, topic=""):
try:
in_topic = self._mqtt_in + topic % self._client
except:
in_topic = self._mqtt_in + topic
_LOGGER.debug("_in_topic: %s" % in_topic)
_LOGGER.debug("_in_topic: %s", in_topic)
return in_topic

2 changes: 1 addition & 1 deletion custom_components/hisense_tv/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "hisense_tv",
"name": "Hisense TV MQTT Bridge",
"version": "21.11.23",
"version": "21.12.28",
"documentation": "https://github.com/sehaas/ha_hisense_tv",
"dependencies": [
"mqtt"
Expand Down
Loading