Skip to content

Commit

Permalink
handle JSONDecodeError & fix pylint warnings
Browse files Browse the repository at this point in the history
- handle None payload
- use lazy substitution during logging
- add minimal pydoc
- refactor some if checks
  • Loading branch information
sehaas committed Dec 30, 2021
1 parent 1a3ea49 commit 7f59297
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 82 deletions.
44 changes: 26 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,6 +185,7 @@ 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)

Expand Down
8 changes: 6 additions & 2 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 @@ -26,6 +28,8 @@ async def get():


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,13 @@ 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
Loading

0 comments on commit 7f59297

Please sign in to comment.