1
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:
David Nielsen 2020-03-09 22:08:31 +05:30 committed by GitHub
parent 19faf06ce7
commit 988b400a9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View File

@ -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
View 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(