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)
예제 #2
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the NAD platform."""
    if config.get(CONF_TYPE) == 'RS232':
        from nad_receiver import NADReceiver
        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':
        from nad_receiver import NADReceiverTelnet
        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)
    else:
        from nad_receiver import NADReceiverTCP
        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)
예제 #3
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the NAD platform."""
    if config.get(CONF_TYPE) == "RS232":
        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,
        )
    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,
        )
class NAD(MediaPlayerEntity):
    """Representation of a NAD Receiver."""

    def __init__(self, config):
        """Initialize the NAD Receiver device."""
        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.config[CONF_NAME]

    @property
    def state(self):
        """Return the state of the device."""
        return self._state

    @property
    def icon(self):
        """Return the icon for the device."""
        return "mdi:speaker-multiple"

    @property
    def volume_level(self):
        """Volume level of the media player (0..1)."""
        return self._volume

    @property
    def is_volume_muted(self):
        """Boolean if volume is currently muted."""
        return self._mute

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        return SUPPORT_NAD

    def turn_off(self):
        """Turn the media player off."""
        self._nad_receiver.main_power("=", "Off")

    def turn_on(self):
        """Turn the media player on."""
        self._nad_receiver.main_power("=", "On")

    def volume_up(self):
        """Volume up the media player."""
        self._nad_receiver.main_volume("+")

    def volume_down(self):
        """Volume down the media player."""
        self._nad_receiver.main_volume("-")

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        self._nad_receiver.main_volume("=", self.calc_db(volume))

    def mute_volume(self, mute):
        """Mute (true) or unmute (false) media player."""
        if mute:
            self._nad_receiver.main_mute("=", "On")
        else:
            self._nad_receiver.main_mute("=", "Off")

    def select_source(self, source):
        """Select input source."""
        self._nad_receiver.main_source("=", self._reverse_mapping.get(source))

    @property
    def source(self):
        """Name of the current input source."""
        return self._source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._reverse_mapping)

    @property
    def available(self):
        """Return if device is available."""
        return self._state is not None

    def update(self) -> None:
        """Retrieve latest state."""
        power_state = self._nad_receiver.main_power("?")
        if not power_state:
            self._state = None
            return
        self._state = (
            STATE_ON if self._nad_receiver.main_power("?") == "On" else STATE_OFF
        )

        if self._state == STATE_ON:
            self._mute = self._nad_receiver.main_mute("?") == "On"
            volume = self._nad_receiver.main_volume("?")
            # Some receivers cannot report the volume, e.g. C 356BEE,
            # instead they only support stepping the volume up or down
            self._volume = self.calc_volume(volume) if volume is not None else None
            self._source = self._source_dict.get(self._nad_receiver.main_source("?"))

    def calc_volume(self, decibel):
        """
        Calculate the volume given the decibel.

        Return the volume (0..1).
        """
        return abs(self._min_volume - decibel) / abs(
            self._min_volume - self._max_volume
        )

    def calc_db(self, volume):
        """
        Calculate the decibel given the volume.

        Return the dB.
        """
        return self._min_volume + round(
            abs(self._min_volume - self._max_volume) * volume
        )