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

Add the Xiaomi TV platform. #12359

Merged
merged 4 commits into from Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -462,6 +462,7 @@ omit =
homeassistant/components/media_player/vizio.py
homeassistant/components/media_player/vlc.py
homeassistant/components/media_player/volumio.py
homeassistant/components/media_player/xiaomi_tv.py
homeassistant/components/media_player/yamaha.py
homeassistant/components/media_player/yamaha_musiccast.py
homeassistant/components/media_player/ziggo_mediabox_xl.py
Expand Down
107 changes: 107 additions & 0 deletions homeassistant/components/media_player/xiaomi_tv.py
@@ -0,0 +1,107 @@
"""
Add support for the Xiaomi TVs.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/xiaomi_tv/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The url is wrong. Look at an existing platform to see the correct format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right yes, I had absolutely missed that. The correct url would be https://home-assistant.io/components/media_player.xiaomi_tv/.

"""

import logging
import voluptuous as vol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a blank line between standard library and 3rd party imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looked at another platform, and you're absolutely right. Will update in next pull request.

import homeassistant.helpers.config_validation as cv
from homeassistant.const import (CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON)
from homeassistant.components.media_player import (
SUPPORT_TURN_ON, SUPPORT_TURN_OFF, MediaPlayerDevice, PLATFORM_SCHEMA,
SUPPORT_VOLUME_STEP)

REQUIREMENTS = ['pymitv==1.0.0']

DEFAULT_NAME="Xiaomi TV"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing whitespace around operator


_LOGGER = logging.getLogger(__name__)

SUPPORT_XIAOMI_TV = SUPPORT_VOLUME_STEP | SUPPORT_TURN_ON | \
SUPPORT_TURN_OFF

# No host is needed for configuration, however it can be set.
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Xiaomi TV platform."""
from pymitv import Discover

# 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.
if not Discover().checkIp(host):
_LOGGER.error(
"Could not find Xiaomi TV with specified IP: %s", host
)
else:
# Register TV with Home Assistant.
add_devices([XiaomiTV(host, name)])
else:
# Otherwise, discover TVs on network.
add_devices(XiaomiTV(tv) for tv in Discover().scan())


class XiaomiTV(MediaPlayerDevice):
"""Represent the Xiaomi TV for Home Assistant."""

def __init__(self, ip, name=DEFAULT_TV):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'DEFAULT_TV'

"""Receive IP address and name to construct class."""
# Import pymitv library.
from pymitv import TV

# Initialize the Xiaomi TV.
self._tv = TV(ip)
# 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 light."""
return self._name

@property
def state(self):
"""Return _state variable, containing the appropriate constant."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

return self._state
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to get the state from the TV?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, and that is the main limitation of this platform. However, as it stands there's two options, when controlling power on the TV:

  • Currently, the TV sleeps and wakes up. This makes for instant turn on and off, however, it sacrifices ability to know state.
  • Another option, is to turn off the TV properly, then we would know the state for sure, but you would't be able to turn it on again. When user then manually turns the TV back on with the remote, it takes a couple of minutes.

I thought the first option sounded better.


@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.
"""
self._tv.sleep()

self._state = STATE_OFF

def turn_on(self):
"""Wake the TV back up from sleep."""
self._tv.wake()

self._state = STATE_ON

def volume_up(self):
"""Increase volume by one."""
self._tv.volume_up()

def volume_down(self):
"""Decrease volume by one."""
self._tv.volume_down()
3 changes: 3 additions & 0 deletions requirements_all.txt
Expand Up @@ -787,6 +787,9 @@ pymailgunner==1.4
# homeassistant.components.media_player.mediaroom
pymediaroom==0.5

# homeassistant.components.media_player.xiaomi_tv
pymitv==1.0.0

# homeassistant.components.mochad
pymochad==0.2.0

Expand Down