mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00: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."""
|
"""Alexa capabilities."""
|
||||||
import logging
|
import logging
|
||||||
import math
|
|
||||||
|
|
||||||
from homeassistant.components import (
|
from homeassistant.components import (
|
||||||
cover,
|
cover,
|
||||||
@ -671,11 +670,8 @@ class AlexaSpeaker(AlexaCapability):
|
|||||||
current_level = self.entity.attributes.get(
|
current_level = self.entity.attributes.get(
|
||||||
media_player.ATTR_MEDIA_VOLUME_LEVEL
|
media_player.ATTR_MEDIA_VOLUME_LEVEL
|
||||||
)
|
)
|
||||||
try:
|
if current_level is not None:
|
||||||
current = math.floor(int(current_level * 100))
|
return round(float(current_level) * 100)
|
||||||
except ZeroDivisionError:
|
|
||||||
current = 0
|
|
||||||
return current
|
|
||||||
|
|
||||||
if name == "muted":
|
if name == "muted":
|
||||||
return bool(
|
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_PAUSE,
|
||||||
SUPPORT_PLAY,
|
SUPPORT_PLAY,
|
||||||
SUPPORT_STOP,
|
SUPPORT_STOP,
|
||||||
|
SUPPORT_VOLUME_MUTE,
|
||||||
|
SUPPORT_VOLUME_SET,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
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):
|
async def test_report_image_processing(hass):
|
||||||
"""Test EventDetectionSensor implements humanPresenceDetectionState property."""
|
"""Test EventDetectionSensor implements humanPresenceDetectionState property."""
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user