Pass config into NAD constructor (#34961)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/36410/head
Frederik Gladhorn 2020-06-03 18:36:59 +02:00 committed by GitHub
parent 9aac8482d5
commit 1186c2c48c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 47 deletions

View File

@ -64,65 +64,42 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the NAD platform."""
if config.get(CONF_TYPE) == "RS232":
if config.get(CONF_TYPE) in ("RS232", "Telnet"):
add_entities(
[
NAD(
config.get(CONF_NAME),
NADReceiver(config.get(CONF_SERIAL_PORT)),
config.get(CONF_MIN_VOLUME),
config.get(CONF_MAX_VOLUME),
config.get(CONF_SOURCE_DICT),
)
],
True,
)
elif config.get(CONF_TYPE) == "Telnet":
add_entities(
[
NAD(
config.get(CONF_NAME),
NADReceiverTelnet(config.get(CONF_HOST), config.get(CONF_PORT)),
config.get(CONF_MIN_VOLUME),
config.get(CONF_MAX_VOLUME),
config.get(CONF_SOURCE_DICT),
)
],
True,
[NAD(config)], True,
)
else:
add_entities(
[
NADtcp(
config.get(CONF_NAME),
NADReceiverTCP(config.get(CONF_HOST)),
config.get(CONF_MIN_VOLUME),
config.get(CONF_MAX_VOLUME),
config.get(CONF_VOLUME_STEP),
)
],
True,
[NADtcp(config)], True,
)
class NAD(MediaPlayerEntity):
"""Representation of a NAD Receiver."""
def __init__(self, name, nad_receiver, min_volume, max_volume, source_dict):
def __init__(self, config):
"""Initialize the NAD Receiver device."""
self._name = name
self._nad_receiver = nad_receiver
self._min_volume = min_volume
self._max_volume = max_volume
self._source_dict = source_dict
self.config = config
self._instantiate_nad_receiver()
self._min_volume = config[CONF_MIN_VOLUME]
self._max_volume = config[CONF_MAX_VOLUME]
self._source_dict = config[CONF_SOURCE_DICT]
self._reverse_mapping = {value: key for key, value in self._source_dict.items()}
self._volume = self._state = self._mute = self._source = None
def _instantiate_nad_receiver(self) -> NADReceiver:
if self.config[CONF_TYPE] == "RS232":
self._nad_receiver = NADReceiver(self.config[CONF_SERIAL_PORT])
else:
host = self.config.get(CONF_HOST)
port = self.config[CONF_PORT]
self._nad_receiver = NADReceiverTelnet(host, port)
@property
def name(self):
"""Return the name of the device."""
return self._name
return self.config[CONF_NAME]
@property
def state(self):
@ -232,13 +209,13 @@ class NAD(MediaPlayerEntity):
class NADtcp(MediaPlayerEntity):
"""Representation of a NAD Digital amplifier."""
def __init__(self, name, nad_device, min_volume, max_volume, volume_step):
def __init__(self, config):
"""Initialize the amplifier."""
self._name = name
self._nad_receiver = nad_device
self._min_vol = (min_volume + 90) * 2 # from dB to nad vol (0-200)
self._max_vol = (max_volume + 90) * 2 # from dB to nad vol (0-200)
self._volume_step = volume_step
self._name = config[CONF_NAME]
self._nad_receiver = NADReceiverTCP(config.get(CONF_HOST))
self._min_vol = (config[CONF_MIN_VOLUME] + 90) * 2 # from dB to nad vol (0-200)
self._max_vol = (config[CONF_MAX_VOLUME] + 90) * 2 # from dB to nad vol (0-200)
self._volume_step = config[CONF_VOLUME_STEP]
self._state = None
self._mute = None
self._nad_volume = None