-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
organize imports / consts authenticate during config flow
- Loading branch information
Showing
8 changed files
with
205 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,186 @@ | ||
import json | ||
import logging | ||
|
||
import voluptuous as vol | ||
from homeassistant import config_entries | ||
from homeassistant.const import CONF_NAME, CONF_MAC | ||
from homeassistant.components import mqtt | ||
from .const import DOMAIN, CONF_MQTT_IN, CONF_MQTT_OUT | ||
from homeassistant.const import CONF_AUTHENTICATION, CONF_MAC, CONF_NAME, CONF_PIN | ||
|
||
import voluptuous as vol | ||
import logging | ||
from .const import ( | ||
CONF_MQTT_IN, | ||
CONF_MQTT_OUT, | ||
DEFAULT_CLIENT_ID, | ||
DEFAULT_MQTT_PREFIX, | ||
DEFAULT_NAME, | ||
DOMAIN, | ||
) | ||
from .helper import mqtt_pub_sub | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class HisenseTvFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Example config flow.""" | ||
"""Hisense TV config flow.""" | ||
|
||
VERSION = 1 | ||
task_mqtt = None | ||
task_auth = None | ||
|
||
def __init__(self): | ||
"""Initialize the config flow.""" | ||
self._mac = None | ||
self._name = None | ||
self._mqtt_in = None | ||
self._mqtt_out = None | ||
self._unsubscribe_auth = None | ||
self._unsubscribe_sourcelist = None | ||
|
||
async def _async_pin_needed(self, message): | ||
_LOGGER.debug("_async_pin_needed") | ||
self._unsubscribe() | ||
self.task_auth = False | ||
self.hass.async_create_task( | ||
self.hass.config_entries.flow.async_configure(flow_id=self.flow_id) | ||
) | ||
|
||
async def _async_pin_not_needed(self, message): | ||
_LOGGER.debug("_async_pin_not_needed") | ||
self._unsubscribe() | ||
self.task_auth = True | ||
self.hass.async_create_task( | ||
self.hass.config_entries.flow.async_configure(flow_id=self.flow_id) | ||
) | ||
|
||
async def _async_authcode_response(self, message): | ||
self._unsubscribe() | ||
payload = json.loads(message.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) | ||
) | ||
|
||
def _unsubscribe(self): | ||
if self._unsubscribe_auth is not None: | ||
self._unsubscribe_auth() | ||
self._unsubscribe_auth = None | ||
if self._unsubscribe_sourcelist is not None: | ||
self._unsubscribe_sourcelist() | ||
self._unsubscribe_sourcelist = None | ||
|
||
async def async_step_user(self, info): | ||
if info is not None: | ||
self._mac = info.get(CONF_MAC) | ||
self._name = info.get(CONF_NAME) | ||
self._mqtt_in = info.get(CONF_MQTT_IN) | ||
self._mqtt_out = info.get(CONF_MQTT_OUT) | ||
return self.async_create_entry( | ||
title=self._name, | ||
data={ | ||
CONF_MAC: self._mac, | ||
CONF_NAME: self._name, | ||
CONF_MQTT_IN: self._mqtt_in, | ||
CONF_MQTT_OUT: self._mqtt_out, | ||
}, | ||
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: | ||
_LOGGER.debug("async_step_user INFO None") | ||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_NAME, default=DEFAULT_NAME): str, | ||
vol.Required(CONF_MAC): str, | ||
vol.Optional(CONF_MQTT_IN, default=DEFAULT_MQTT_PREFIX): str, | ||
vol.Optional(CONF_MQTT_OUT, default=DEFAULT_MQTT_PREFIX): str, | ||
} | ||
), | ||
) | ||
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), | ||
} | ||
|
||
default_mqtt_in = self._mqtt_in or "hisense" | ||
default_mqtt_out = self._mqtt_out or "hisense" | ||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_MAC): str, | ||
vol.Required(CONF_NAME, default=self._name): str, | ||
vol.Optional(CONF_MQTT_IN, default=default_mqtt_in): str, | ||
vol.Optional(CONF_MQTT_OUT, default=default_mqtt_out): str, | ||
} | ||
), | ||
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( | ||
hass=self.hass, | ||
topic="%s/remoteapp/mobile/%s/ui_service/data/authentication" | ||
% (self.task_mqtt.get(CONF_MQTT_IN), client_id), | ||
msg_callback=self._async_pin_needed, | ||
) | ||
self._unsubscribe_sourcelist = await mqtt.async_subscribe( | ||
hass=self.hass, | ||
topic="%s/remoteapp/mobile/%s/ui_service/data/sourcelist" | ||
% (self.task_mqtt.get(CONF_MQTT_IN), client_id), | ||
msg_callback=self._async_pin_not_needed, | ||
) | ||
mqtt.publish( | ||
hass=self.hass, | ||
topic="%s/remoteapp/tv/ui_service/%s/actions/gettvstate" | ||
% (self.task_mqtt.get(CONF_MQTT_OUT), client_id), | ||
payload="", | ||
) | ||
mqtt.publish( | ||
hass=self.hass, | ||
topic="%s/remoteapp/tv/ui_service/%s/actions/sourcelist" | ||
% (self.task_mqtt.get(CONF_MQTT_OUT), client_id), | ||
payload="", | ||
) | ||
|
||
async def async_step_reauth(self, user_input=None): | ||
self.task_auth = None | ||
return await self.async_step_auth(user_input=user_input) | ||
|
||
async def async_step_auth(self, user_input=None): | ||
if self.task_auth is True: | ||
_LOGGER.debug("async_step_auth finish") | ||
return self.async_show_progress_done(next_step_id="finish") | ||
|
||
if self.task_auth is False: | ||
_LOGGER.debug("async_step_auth reauth") | ||
return self.async_show_progress_done(next_step_id="reauth") | ||
|
||
if user_input is None: | ||
self.task_auth = None | ||
_LOGGER.debug("async_step_auth show form") | ||
return self.async_show_form( | ||
step_id="auth", | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_PIN): int, | ||
} | ||
), | ||
) | ||
else: | ||
_LOGGER.debug("async_step_auth send authentication") | ||
client_id = DEFAULT_CLIENT_ID | ||
self._unsubscribe_auth = await mqtt.async_subscribe( | ||
hass=self.hass, | ||
topic="%s/remoteapp/mobile/%s/ui_service/data/authenticationcode" | ||
% (self.task_mqtt.get(CONF_MQTT_IN), client_id), | ||
msg_callback=self._async_authcode_response, | ||
) | ||
payload = json.dumps({"authNum": user_input.get(CONF_PIN)}) | ||
mqtt.publish( | ||
hass=self.hass, | ||
topic="%s/remoteapp/tv/ui_service/%s/actions/authenticationcode" | ||
% (self.task_mqtt.get(CONF_MQTT_OUT), client_id), | ||
payload=payload, | ||
) | ||
return self.async_show_progress( | ||
step_id="auth", | ||
progress_action="progress_action", | ||
) | ||
|
||
async def async_step_finish(self, user_input=None): | ||
_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.warn("async_step_import") | ||
_LOGGER.debug("async_step_import") | ||
return self.async_create_entry(title=data[CONF_NAME], data=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
DEFAULT_NAME = "Hisense TV" | ||
DOMAIN = "hisense_tv" | ||
DATA_KEY = "media_player.hisense_tv" | ||
|
||
ATTR_CODE = "auth_code" | ||
CONF_MQTT_IN = "mqtt_in" | ||
CONF_MQTT_OUT = "mqtt_out" | ||
|
||
SERVICE_START_AUTHENTICATION = "start_authentication" | ||
SERVICE_AUTHENTICATE = "authenticate_client" | ||
ATTR_CODE = "auth_code" | ||
DATA_KEY = "media_player.hisense_tv" | ||
DEFAULT_CLIENT_ID = "HomeAssistant" | ||
DEFAULT_MQTT_PREFIX = "hisense" | ||
DEFAULT_NAME = "Hisense TV" | ||
DOMAIN = "hisense_tv" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import asyncio | ||
|
||
from homeassistant.components import mqtt | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.