2019-04-03 15:40:03 +00:00
|
|
|
"""Add support for the Xiaomi TVs."""
|
2018-02-16 23:09:20 +00:00
|
|
|
import logging
|
2018-09-09 12:26:06 +00:00
|
|
|
|
2019-11-25 23:40:42 +00:00
|
|
|
import pymitv
|
2019-12-09 13:58:51 +00:00
|
|
|
import voluptuous as vol
|
2018-09-09 12:26:06 +00:00
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_TURN_OFF,
|
|
|
|
SUPPORT_TURN_ON,
|
|
|
|
SUPPORT_VOLUME_STEP,
|
|
|
|
)
|
2018-09-09 12:26:06 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-02-16 23:09:20 +00:00
|
|
|
|
|
|
|
DEFAULT_NAME = "Xiaomi TV"
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_XIAOMI_TV = SUPPORT_VOLUME_STEP | SUPPORT_TURN_ON | SUPPORT_TURN_OFF
|
2018-02-16 23:09:20 +00:00
|
|
|
|
|
|
|
# No host is needed for configuration, however it can be set.
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2018-02-16 23:09:20 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-02-16 23:09:20 +00:00
|
|
|
"""Set up the Xiaomi TV platform."""
|
|
|
|
|
|
|
|
# If a hostname is set. Discovery is skipped.
|
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
|
|
|
if host is not None:
|
|
|
|
# Check if there's a valid TV at the IP address.
|
2019-11-25 23:40:42 +00:00
|
|
|
if not pymitv.Discover().check_ip(host):
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Could not find Xiaomi TV with specified IP: %s", host)
|
2018-02-16 23:09:20 +00:00
|
|
|
else:
|
|
|
|
# Register TV with Home Assistant.
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([XiaomiTV(host, name)])
|
2018-02-16 23:09:20 +00:00
|
|
|
else:
|
|
|
|
# Otherwise, discover TVs on network.
|
2019-11-25 23:40:42 +00:00
|
|
|
add_entities(XiaomiTV(tv, DEFAULT_NAME) for tv in pymitv.Discover().scan())
|
2018-02-16 23:09:20 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
class XiaomiTV(MediaPlayerEntity):
|
2018-02-16 23:09:20 +00:00
|
|
|
"""Represent the Xiaomi TV for Home Assistant."""
|
|
|
|
|
|
|
|
def __init__(self, ip, name):
|
|
|
|
"""Receive IP address and name to construct class."""
|
|
|
|
|
|
|
|
# Initialize the Xiaomi TV.
|
2019-11-25 23:40:42 +00:00
|
|
|
self._tv = pymitv.TV(ip)
|
2018-02-16 23:09:20 +00:00
|
|
|
# Default name value, only to be overridden by user.
|
|
|
|
self._name = name
|
|
|
|
self._state = STATE_OFF
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this TV."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return _state variable, containing the appropriate constant."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Indicate that state is assumed."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
|
|
|
return SUPPORT_XIAOMI_TV
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""
|
|
|
|
Instruct the TV to turn sleep.
|
|
|
|
|
|
|
|
This is done instead of turning off,
|
|
|
|
because the TV won't accept any input when turned off. Thus, the user
|
|
|
|
would be unable to turn the TV back on, unless it's done manually.
|
|
|
|
"""
|
2020-05-08 22:10:17 +00:00
|
|
|
if self._state != STATE_OFF:
|
2018-12-27 16:38:07 +00:00
|
|
|
self._tv.sleep()
|
2018-02-16 23:09:20 +00:00
|
|
|
|
2018-12-27 16:38:07 +00:00
|
|
|
self._state = STATE_OFF
|
2018-02-16 23:09:20 +00:00
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Wake the TV back up from sleep."""
|
2020-05-08 22:10:17 +00:00
|
|
|
if self._state != STATE_ON:
|
2018-12-27 16:38:07 +00:00
|
|
|
self._tv.wake()
|
2018-02-16 23:09:20 +00:00
|
|
|
|
2018-12-27 16:38:07 +00:00
|
|
|
self._state = STATE_ON
|
2018-02-16 23:09:20 +00:00
|
|
|
|
|
|
|
def volume_up(self):
|
|
|
|
"""Increase volume by one."""
|
|
|
|
self._tv.volume_up()
|
|
|
|
|
|
|
|
def volume_down(self):
|
|
|
|
"""Decrease volume by one."""
|
|
|
|
self._tv.volume_down()
|