mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Use _attr in onkyo media player (#82832)
This commit is contained in:
parent
9ecbcd2d8f
commit
19abba7f6b
@ -252,27 +252,25 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
):
|
||||
"""Initialize the Onkyo Receiver."""
|
||||
self._receiver = receiver
|
||||
self._muted = False
|
||||
self._volume = 0
|
||||
self._pwstate = MediaPlayerState.OFF
|
||||
self._attr_is_volume_muted = False
|
||||
self._attr_volume_level = 0
|
||||
self._attr_state = MediaPlayerState.OFF
|
||||
if name:
|
||||
# not discovered
|
||||
self._name = name
|
||||
self._unique_id = None
|
||||
self._attr_name = name
|
||||
else:
|
||||
# discovered
|
||||
self._unique_id = (
|
||||
self._attr_unique_id = (
|
||||
f"{receiver.info['model_name']}_{receiver.info['identifier']}"
|
||||
)
|
||||
self._name = self._unique_id
|
||||
self._attr_name = self._attr_unique_id
|
||||
|
||||
self._max_volume = max_volume
|
||||
self._receiver_max_volume = receiver_max_volume
|
||||
self._current_source = None
|
||||
self._source_list = list(sources.values())
|
||||
self._attr_source_list = list(sources.values())
|
||||
self._source_mapping = sources
|
||||
self._reverse_mapping = {value: key for key, value in sources.items()}
|
||||
self._attributes = {}
|
||||
self._attr_extra_state_attributes = {}
|
||||
self._hdmi_out_supported = True
|
||||
self._audio_info_supported = True
|
||||
self._video_info_supported = True
|
||||
@ -284,9 +282,9 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
except (ValueError, OSError, AttributeError, AssertionError):
|
||||
if self._receiver.command_socket:
|
||||
self._receiver.command_socket = None
|
||||
_LOGGER.debug("Resetting connection to %s", self._name)
|
||||
_LOGGER.debug("Resetting connection to %s", self.name)
|
||||
else:
|
||||
_LOGGER.info("%s is disconnected. Attempting to reconnect", self._name)
|
||||
_LOGGER.info("%s is disconnected. Attempting to reconnect", self.name)
|
||||
return False
|
||||
_LOGGER.debug("Result for %s: %s", command, result)
|
||||
return result
|
||||
@ -298,13 +296,13 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
if not status:
|
||||
return
|
||||
if status[1] == "on":
|
||||
self._pwstate = MediaPlayerState.ON
|
||||
self._attr_state = MediaPlayerState.ON
|
||||
else:
|
||||
self._pwstate = MediaPlayerState.OFF
|
||||
self._attributes.pop(ATTR_AUDIO_INFORMATION, None)
|
||||
self._attributes.pop(ATTR_VIDEO_INFORMATION, None)
|
||||
self._attributes.pop(ATTR_PRESET, None)
|
||||
self._attributes.pop(ATTR_VIDEO_OUT, None)
|
||||
self._attr_state = MediaPlayerState.OFF
|
||||
self._attr_extra_state_attributes.pop(ATTR_AUDIO_INFORMATION, None)
|
||||
self._attr_extra_state_attributes.pop(ATTR_VIDEO_INFORMATION, None)
|
||||
self._attr_extra_state_attributes.pop(ATTR_PRESET, None)
|
||||
self._attr_extra_state_attributes.pop(ATTR_VIDEO_OUT, None)
|
||||
return
|
||||
|
||||
volume_raw = self.command("volume query")
|
||||
@ -331,67 +329,27 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
|
||||
for source in sources:
|
||||
if source in self._source_mapping:
|
||||
self._current_source = self._source_mapping[source]
|
||||
self._attr_source = self._source_mapping[source]
|
||||
break
|
||||
self._current_source = "_".join(sources)
|
||||
self._attr_source = "_".join(sources)
|
||||
|
||||
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 preset_raw and self.source and self.source.lower() == "radio":
|
||||
self._attr_extra_state_attributes[ATTR_PRESET] = preset_raw[1]
|
||||
elif ATTR_PRESET in self._attr_extra_state_attributes:
|
||||
del self._attr_extra_state_attributes[ATTR_PRESET]
|
||||
|
||||
self._muted = bool(mute_raw[1] == "on")
|
||||
self._attr_is_volume_muted = bool(mute_raw[1] == "on")
|
||||
# AMP_VOL/MAX_RECEIVER_VOL*(MAX_VOL/100)
|
||||
self._volume = volume_raw[1] / (
|
||||
self._attr_volume_level = volume_raw[1] / (
|
||||
self._receiver_max_volume * self._max_volume / 100
|
||||
)
|
||||
|
||||
if not hdmi_out_raw:
|
||||
return
|
||||
self._attributes[ATTR_VIDEO_OUT] = ",".join(hdmi_out_raw[1])
|
||||
self._attr_extra_state_attributes[ATTR_VIDEO_OUT] = ",".join(hdmi_out_raw[1])
|
||||
if hdmi_out_raw[1] == "N/A":
|
||||
self._hdmi_out_supported = False
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID for this device."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._pwstate
|
||||
|
||||
@property
|
||||
def volume_level(self):
|
||||
"""Return the volume level of the media player (0..1)."""
|
||||
return self._volume
|
||||
|
||||
@property
|
||||
def is_volume_muted(self):
|
||||
"""Return boolean indicating mute status."""
|
||||
return self._muted
|
||||
|
||||
@property
|
||||
def source(self):
|
||||
"""Return the current input source of the device."""
|
||||
return self._current_source
|
||||
|
||||
@property
|
||||
def source_list(self):
|
||||
"""List of available input sources."""
|
||||
return self._source_list
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return device specific state attributes."""
|
||||
return self._attributes
|
||||
|
||||
def turn_off(self) -> None:
|
||||
"""Turn the media player off."""
|
||||
self.command("system-power standby")
|
||||
@ -431,13 +389,13 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
|
||||
def select_source(self, source: str) -> None:
|
||||
"""Set the input source."""
|
||||
if source in self._source_list:
|
||||
if self.source_list and source in self.source_list:
|
||||
source = self._reverse_mapping[source]
|
||||
self.command(f"input-selector {source}")
|
||||
|
||||
def play_media(self, media_type: str, media_id: str, **kwargs: Any) -> None:
|
||||
"""Play radio station by preset number."""
|
||||
source = self._reverse_mapping[self._current_source]
|
||||
source = self._reverse_mapping[self._attr_source]
|
||||
if media_type.lower() == "radio" and source in DEFAULT_PLAYABLE_SOURCES:
|
||||
self.command(f"preset {media_id}")
|
||||
|
||||
@ -460,9 +418,9 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
"output_channels": _tuple_get(values, 5),
|
||||
"output_frequency": _tuple_get(values, 6),
|
||||
}
|
||||
self._attributes[ATTR_AUDIO_INFORMATION] = info
|
||||
self._attr_extra_state_attributes[ATTR_AUDIO_INFORMATION] = info
|
||||
else:
|
||||
self._attributes.pop(ATTR_AUDIO_INFORMATION, None)
|
||||
self._attr_extra_state_attributes.pop(ATTR_AUDIO_INFORMATION, None)
|
||||
|
||||
def _parse_video_information(self, video_information_raw):
|
||||
values = _parse_onkyo_payload(video_information_raw)
|
||||
@ -480,9 +438,9 @@ class OnkyoDevice(MediaPlayerEntity):
|
||||
"output_color_depth": _tuple_get(values, 7),
|
||||
"picture_mode": _tuple_get(values, 8),
|
||||
}
|
||||
self._attributes[ATTR_VIDEO_INFORMATION] = info
|
||||
self._attr_extra_state_attributes[ATTR_VIDEO_INFORMATION] = info
|
||||
else:
|
||||
self._attributes.pop(ATTR_VIDEO_INFORMATION, None)
|
||||
self._attr_extra_state_attributes.pop(ATTR_VIDEO_INFORMATION, None)
|
||||
|
||||
|
||||
class OnkyoDeviceZone(OnkyoDevice):
|
||||
@ -509,9 +467,9 @@ class OnkyoDeviceZone(OnkyoDevice):
|
||||
if not status:
|
||||
return
|
||||
if status[1] == "on":
|
||||
self._pwstate = MediaPlayerState.ON
|
||||
self._attr_state = MediaPlayerState.ON
|
||||
else:
|
||||
self._pwstate = MediaPlayerState.OFF
|
||||
self._attr_state = MediaPlayerState.OFF
|
||||
return
|
||||
|
||||
volume_raw = self.command(f"zone{self._zone}.volume=query")
|
||||
@ -538,17 +496,17 @@ class OnkyoDeviceZone(OnkyoDevice):
|
||||
|
||||
for source in current_source_tuples[1]:
|
||||
if source in self._source_mapping:
|
||||
self._current_source = self._source_mapping[source]
|
||||
self._attr_source = self._source_mapping[source]
|
||||
break
|
||||
self._current_source = "_".join(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]
|
||||
self._attr_source = "_".join(current_source_tuples[1])
|
||||
self._attr_is_volume_muted = bool(mute_raw[1] == "on")
|
||||
if preset_raw and self.source and self.source.lower() == "radio":
|
||||
self._attr_extra_state_attributes[ATTR_PRESET] = preset_raw[1]
|
||||
elif ATTR_PRESET in self._attr_extra_state_attributes:
|
||||
del self._attr_extra_state_attributes[ATTR_PRESET]
|
||||
if self._supports_volume:
|
||||
# AMP_VOL/MAX_RECEIVER_VOL*(MAX_VOL/100)
|
||||
self._volume = (
|
||||
self._attr_volume_level = (
|
||||
volume_raw[1] / self._receiver_max_volume * (self._max_volume / 100)
|
||||
)
|
||||
|
||||
@ -598,6 +556,6 @@ class OnkyoDeviceZone(OnkyoDevice):
|
||||
|
||||
def select_source(self, source: str) -> None:
|
||||
"""Set the input source."""
|
||||
if source in self._source_list:
|
||||
if self.source_list and source in self.source_list:
|
||||
source = self._reverse_mapping[source]
|
||||
self.command(f"zone{self._zone}.selector={source}")
|
||||
|
Loading…
x
Reference in New Issue
Block a user