mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Fixed duplicate import statements and made use of the config_helper
This commit is contained in:
parent
5719743ec7
commit
1c10f218de
@ -18,6 +18,8 @@ from homeassistant.const import (
|
|||||||
CONF_HOST, CONF_NAME, STATE_OFF,
|
CONF_HOST, CONF_NAME, STATE_OFF,
|
||||||
STATE_ON, STATE_UNKNOWN)
|
STATE_ON, STATE_UNKNOWN)
|
||||||
|
|
||||||
|
from homeassistant.helpers import validate_config
|
||||||
|
|
||||||
CONF_PORT = "port"
|
CONF_PORT = "port"
|
||||||
CONF_TIMEOUT = "timeout"
|
CONF_TIMEOUT = "timeout"
|
||||||
|
|
||||||
@ -34,13 +36,13 @@ SUPPORT_SAMSUNGTV = SUPPORT_PAUSE | SUPPORT_VOLUME_STEP | \
|
|||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
""" Sets up the Samsung TV platform. """
|
""" Sets up the Samsung TV platform. """
|
||||||
|
|
||||||
if not config.get(CONF_HOST):
|
# Validate that all required config options are given
|
||||||
_LOGGER.error(
|
if not validate_config({DOMAIN: config}, {DOMAIN: [CONF_HOST]}, _LOGGER):
|
||||||
"Missing required configuration items in %s: %s",
|
|
||||||
DOMAIN,
|
|
||||||
CONF_HOST)
|
|
||||||
return False
|
return False
|
||||||
name = config.get("name", 'Samsung TV Remote')
|
|
||||||
|
# Default the entity_name to 'Samsung TV Remote'
|
||||||
|
name = config.get(CONF_NAME, 'Samsung TV Remote')
|
||||||
|
|
||||||
# Generate a config for the Samsung lib
|
# Generate a config for the Samsung lib
|
||||||
remote_config = {
|
remote_config = {
|
||||||
"name": "HomeAssistant",
|
"name": "HomeAssistant",
|
||||||
@ -60,7 +62,9 @@ class SamsungTVDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
# pylint: disable=too-many-public-methods
|
# pylint: disable=too-many-public-methods
|
||||||
def __init__(self, name, config):
|
def __init__(self, name, config):
|
||||||
|
from samsungctl import Remote
|
||||||
|
# Save a reference to the imported class
|
||||||
|
self._remote_class = Remote
|
||||||
self._name = name
|
self._name = name
|
||||||
# Assume that the TV is not muted
|
# Assume that the TV is not muted
|
||||||
self._muted = False
|
self._muted = False
|
||||||
@ -76,28 +80,26 @@ class SamsungTVDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
def get_remote(self):
|
def get_remote(self):
|
||||||
""" Creates or Returns a remote control instance """
|
""" Creates or Returns a remote control instance """
|
||||||
from samsungctl import Remote
|
|
||||||
|
|
||||||
if self._remote is None:
|
if self._remote is None:
|
||||||
# We need to create a new instance to reconnect.
|
# We need to create a new instance to reconnect.
|
||||||
self._remote = Remote(self._config)
|
self._remote = self._remote_class(self._config)
|
||||||
|
|
||||||
return self._remote
|
return self._remote
|
||||||
|
|
||||||
def send_key(self, key):
|
def send_key(self, key):
|
||||||
""" Sends a key to the tv and handles exceptions """
|
""" Sends a key to the tv and handles exceptions """
|
||||||
from samsungctl import Remote
|
|
||||||
try:
|
try:
|
||||||
self.get_remote().control(key)
|
self.get_remote().control(key)
|
||||||
self._state = STATE_ON
|
self._state = STATE_ON
|
||||||
except (Remote.UnhandledResponse, Remote.AccessDenied,
|
except (self._remote_class.UnhandledResponse,
|
||||||
BrokenPipeError):
|
self._remote_class.AccessDenied, BrokenPipeError):
|
||||||
# We got a response so it's on.
|
# We got a response so it's on.
|
||||||
# BrokenPipe can occur when the commands is sent to fast
|
# BrokenPipe can occur when the commands is sent to fast
|
||||||
self._state = STATE_ON
|
self._state = STATE_ON
|
||||||
self._remote = None
|
self._remote = None
|
||||||
return False
|
return False
|
||||||
except (Remote.ConnectionClosed, socket.timeout,
|
except (self._remote_class.ConnectionClosed, socket.timeout,
|
||||||
TimeoutError, OSError):
|
TimeoutError, OSError):
|
||||||
self._state = STATE_OFF
|
self._state = STATE_OFF
|
||||||
self._remote = None
|
self._remote = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user