Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sehaas committed Mar 24, 2021
0 parents commit 0e11904
Show file tree
Hide file tree
Showing 10 changed files with 876 additions and 0 deletions.
49 changes: 49 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Component init"""
import asyncio
import logging

from .const import DOMAIN

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

_LOGGER = logging.getLogger(__name__)

PLATFORMS = ["media_player"]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up HisenseTV from a config entry."""
_LOGGER.debug("async_setup_entry")

hass.data[DOMAIN][entry.entry_id] = {}

for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)

return True


async def async_unload_entry(hass, entry):
"""Unload HisenseTV config entry."""
_LOGGER.debug("async_unload_entry")
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok


async def async_setup(hass, config):
"""Set up the HisenseTV integration."""
_LOGGER.debug("async_setup")
hass.data.setdefault(DOMAIN, {})
return True
55 changes: 55 additions & 0 deletions config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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

import voluptuous as vol
import logging

_LOGGER = logging.getLogger(__name__)


class HisenseTvFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Example config flow."""

def __init__(self):
"""Initialize the config flow."""
self._mac = None
self._name = None
self._mqtt_in = None
self._mqtt_out = 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,
},
)

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,
}
),
)

async def async_step_import(self, data):
"""Handle import from YAML."""
_LOGGER.warn("async_step_import")
return self.async_create_entry(title=data[CONF_NAME], data=data)
10 changes: 10 additions & 0 deletions const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DEFAULT_NAME = "Hisense TV"
DOMAIN = "hisense_tv"
DATA_KEY = "media_player.hisense_tv"

CONF_MQTT_IN = "mqtt_in"
CONF_MQTT_OUT = "mqtt_out"

SERVICE_START_AUTHENTICATION = "start_authentication"
SERVICE_AUTHENTICATE = "authenticate_client"
ATTR_CODE = "auth_code"
18 changes: 18 additions & 0 deletions helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import asyncio
from homeassistant.components import mqtt


async def mqtt_pub_sub(hass, pub, sub, payload=""):
loop = asyncio.get_event_loop()
queue = asyncio.Queue()

def put(*args):
loop.call_soon_threadsafe(queue.put_nowait, args)

async def get():
while True:
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)
return get(), unsubscribe
11 changes: 11 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"domain": "hisense_tv",
"name": "Hisense TV MQTT Bridge",
"documentation": "https://github.com/sehaas/ha_hisense",
"dependencies": ["mqtt"],
"codeowners": [
"@sehaas"
],
"requirements": ["wakeonlan==1.1.6"],
"config_flow": true
}
Loading

0 comments on commit 0e11904

Please sign in to comment.