Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
Fetch Onkyo current radio preset (home-assistant#26211)
Browse files Browse the repository at this point in the history
* atribute to show which preset is currently on in radio

* add attribute for onkyo zone

* change format string to f-strings
  • Loading branch information
pszafer authored and MartinHjelmare committed Aug 31, 2019
1 parent 9df2c3f commit 922522b
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions homeassistant/components/onkyo/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@

TIMEOUT_MESSAGE = "Timeout waiting for response."


ATTR_HDMI_OUTPUT = "hdmi_output"
ATTR_PRESET = "preset"

ACCEPTED_VALUES = [
"no",
"analog",
Expand Down Expand Up @@ -177,7 +180,7 @@ def service_handle(service):
"2",
receiver,
config.get(CONF_SOURCES),
name="{} Zone 2".format(config[CONF_NAME]),
name=f"{config[CONF_NAME]} Zone 2",
)
)
# Add Zone3 if available
Expand All @@ -188,7 +191,7 @@ def service_handle(service):
"3",
receiver,
config.get(CONF_SOURCES),
name="{} Zone 3".format(config[CONF_NAME]),
name=f"{config[CONF_NAME]} Zone 3",
)
)
except OSError:
Expand All @@ -210,8 +213,8 @@ def __init__(self, receiver, sources, name=None, max_volume=SUPPORTED_MAX_VOLUME
self._muted = False
self._volume = 0
self._pwstate = STATE_OFF
self._name = name or "{}_{}".format(
receiver.info["model_name"], receiver.info["identifier"]
self._name = (
name or f"{receiver.info['model_name']}_{receiver.info['identifier']}"
)
self._max_volume = max_volume
self._current_source = None
Expand Down Expand Up @@ -249,7 +252,7 @@ def update(self):
mute_raw = self.command("audio-muting query")
current_source_raw = self.command("input-selector query")
hdmi_out_raw = self.command("hdmi-output-selector query")

preset_raw = self.command("preset query")
if not (volume_raw and mute_raw and current_source_raw):
return

Expand All @@ -265,6 +268,11 @@ def update(self):
break
else:
self._current_source = "_".join([i for i in current_source_tuples[1]])
if preset_raw and self._current_source.lower() == "radio":
self._attributes[ATTR_PRESET] = preset_raw[1]
elif ATTR_PRESET in self._attributes:
del self._attributes[ATTR_PRESET]

self._muted = bool(mute_raw[1] == "on")
self._volume = volume_raw[1] / self._max_volume

Expand Down Expand Up @@ -323,7 +331,7 @@ def set_volume_level(self, volume):
Onkyo ranges from 1-80 however 80 is usually far too loud
so allow the user to specify the upper range with CONF_MAX_VOLUME
"""
self.command("volume {}".format(int(volume * self._max_volume)))
self.command(f"volume {int(volume * self._max_volume)}")

def volume_up(self):
"""Increase volume by 1 step."""
Expand All @@ -348,17 +356,17 @@ def select_source(self, source):
"""Set the input source."""
if source in self._source_list:
source = self._reverse_mapping[source]
self.command("input-selector {}".format(source))
self.command(f"input-selector {source}")

def play_media(self, media_type, media_id, **kwargs):
"""Play radio station by preset number."""
source = self._reverse_mapping[self._current_source]
if media_type.lower() == "radio" and source in DEFAULT_PLAYABLE_SOURCES:
self.command("preset {}".format(media_id))
self.command(f"preset {media_id}")

def select_output(self, output):
"""Set hdmi-out."""
self.command("hdmi-output-selector={}".format(output))
self.command(f"hdmi-output-selector={output}")


class OnkyoDeviceZone(OnkyoDevice):
Expand All @@ -372,7 +380,7 @@ def __init__(self, zone, receiver, sources, name=None):

def update(self):
"""Get the latest state from the device."""
status = self.command("zone{}.power=query".format(self._zone))
status = self.command(f"zone{self._zone}.power=query")

if not status:
return
Expand All @@ -382,10 +390,10 @@ def update(self):
self._pwstate = STATE_OFF
return

volume_raw = self.command("zone{}.volume=query".format(self._zone))
mute_raw = self.command("zone{}.muting=query".format(self._zone))
current_source_raw = self.command("zone{}.selector=query".format(self._zone))

volume_raw = self.command(f"zone{self._zone}.volume=query")
mute_raw = self.command(f"zone{self._zone}.muting=query")
current_source_raw = self.command(f"zone{self._zone}.selector=query")
preset_raw = self.command(f"zone{self._zone}.preset=query")
# If we received a source value, but not a volume value
# it's likely this zone permanently does not support volume.
if current_source_raw and not volume_raw:
Expand All @@ -411,7 +419,10 @@ def update(self):
else:
self._current_source = "_".join([i for i in current_source_tuples[1]])
self._muted = bool(mute_raw[1] == "on")

if preset_raw and self._current_source.lower() == "radio":
self._attributes[ATTR_PRESET] = preset_raw[1]
elif ATTR_PRESET in self._attributes:
del self._attributes[ATTR_PRESET]
if self._supports_volume:
self._volume = volume_raw[1] / 80.0

Expand All @@ -424,33 +435,33 @@ def supported_features(self):

def turn_off(self):
"""Turn the media player off."""
self.command("zone{}.power=standby".format(self._zone))
self.command(f"zone{self._zone}.power=standby")

def set_volume_level(self, volume):
"""Set volume level, input is range 0..1. Onkyo ranges from 1-80."""
self.command("zone{}.volume={}".format(self._zone, int(volume * 80)))
self.command(f"zone{self._zone}.volume={int(volume * 80)}")

def volume_up(self):
"""Increase volume by 1 step."""
self.command("zone{}.volume=level-up".format(self._zone))
self.command(f"zone{self._zone}.volume=level-up")

def volume_down(self):
"""Decrease volume by 1 step."""
self.command("zone{}.volume=level-down".format(self._zone))
self.command(f"zone{self._zone}.volume=level-down")

def mute_volume(self, mute):
"""Mute (true) or unmute (false) media player."""
if mute:
self.command("zone{}.muting=on".format(self._zone))
self.command(f"zone{self._zone}.muting=on")
else:
self.command("zone{}.muting=off".format(self._zone))
self.command(f"zone{self._zone}.muting=off")

def turn_on(self):
"""Turn the media player on."""
self.command("zone{}.power=on".format(self._zone))
self.command(f"zone{self._zone}.power=on")

def select_source(self, source):
"""Set the input source."""
if source in self._source_list:
source = self._reverse_mapping[source]
self.command("zone{}.selector={}".format(self._zone, source))
self.command(f"zone{self._zone}.selector={source}")

0 comments on commit 922522b

Please sign in to comment.