mirror of
https://github.com/home-assistant/core
synced 2024-10-04 07:58:43 +02:00
Fix TypeError Exception in AlexaSpeaker (#32318)
* alexa/capabilities.py: Fix TypeError Exception - Remove division by zero try/catch -- there is no division - Handle TypeError exception when current_volume = None - Simplify math and return logic * Add test for Alexa.Speaker's valid volume range
This commit is contained in:
parent
19faf06ce7
commit
988b400a9c
@ -1,6 +1,5 @@
|
||||
"""Alexa capabilities."""
|
||||
import logging
|
||||
import math
|
||||
|
||||
from homeassistant.components import (
|
||||
cover,
|
||||
@ -671,11 +670,8 @@ class AlexaSpeaker(AlexaCapability):
|
||||
current_level = self.entity.attributes.get(
|
||||
media_player.ATTR_MEDIA_VOLUME_LEVEL
|
||||
)
|
||||
try:
|
||||
current = math.floor(int(current_level * 100))
|
||||
except ZeroDivisionError:
|
||||
current = 0
|
||||
return current
|
||||
if current_level is not None:
|
||||
return round(float(current_level) * 100)
|
||||
|
||||
if name == "muted":
|
||||
return bool(
|
||||
|
32
tests/components/alexa/test_capabilities.py
Normal file → Executable file
32
tests/components/alexa/test_capabilities.py
Normal file → Executable file
@ -8,6 +8,8 @@ from homeassistant.components.media_player.const import (
|
||||
SUPPORT_PAUSE,
|
||||
SUPPORT_PLAY,
|
||||
SUPPORT_STOP,
|
||||
SUPPORT_VOLUME_MUTE,
|
||||
SUPPORT_VOLUME_SET,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
@ -684,6 +686,36 @@ async def test_report_playback_state(hass):
|
||||
)
|
||||
|
||||
|
||||
async def test_report_speaker_volume(hass):
|
||||
"""Test Speaker reports volume correctly."""
|
||||
hass.states.async_set(
|
||||
"media_player.test_speaker",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Test media player speaker",
|
||||
"supported_features": SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET,
|
||||
"volume_level": None,
|
||||
"device_class": "speaker",
|
||||
},
|
||||
)
|
||||
properties = await reported_properties(hass, "media_player.test_speaker")
|
||||
properties.assert_not_has_property("Alexa.Speaker", "volume")
|
||||
|
||||
for good_value in range(101):
|
||||
hass.states.async_set(
|
||||
"media_player.test_speaker",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Test media player speaker",
|
||||
"supported_features": SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET,
|
||||
"volume_level": good_value / 100,
|
||||
"device_class": "speaker",
|
||||
},
|
||||
)
|
||||
properties = await reported_properties(hass, "media_player.test_speaker")
|
||||
properties.assert_equal("Alexa.Speaker", "volume", good_value)
|
||||
|
||||
|
||||
async def test_report_image_processing(hass):
|
||||
"""Test EventDetectionSensor implements humanPresenceDetectionState property."""
|
||||
hass.states.async_set(
|
||||
|
Loading…
Reference in New Issue
Block a user