diff --git a/homeassistant/components/alexa/capabilities.py b/homeassistant/components/alexa/capabilities.py index dfb97cd9db2..d769f797da1 100644 --- a/homeassistant/components/alexa/capabilities.py +++ b/homeassistant/components/alexa/capabilities.py @@ -11,6 +11,7 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, STATE_UNLOCKED, + STATE_UNKNOWN, ) import homeassistant.components.climate.const as climate from homeassistant.components import light, fan, cover @@ -443,7 +444,17 @@ class AlexaTemperatureSensor(AlexaCapibility): if self.entity.domain == climate.DOMAIN: unit = self.hass.config.units.temperature_unit temp = self.entity.attributes.get(climate.ATTR_CURRENT_TEMPERATURE) - return {"value": float(temp), "scale": API_TEMP_UNITS[unit]} + + if temp in (STATE_UNAVAILABLE, STATE_UNKNOWN): + return None + + try: + temp = float(temp) + except ValueError: + _LOGGER.warning("Invalid temp value %s for %s", temp, self.entity.entity_id) + return None + + return {"value": temp, "scale": API_TEMP_UNITS[unit]} class AlexaContactSensor(AlexaCapibility): @@ -591,4 +602,12 @@ class AlexaThermostatController(AlexaCapibility): if temp is None: return None - return {"value": float(temp), "scale": API_TEMP_UNITS[unit]} + try: + temp = float(temp) + except ValueError: + _LOGGER.warning( + "Invalid temp value %s for %s in %s", temp, name, self.entity.entity_id + ) + return None + + return {"value": temp, "scale": API_TEMP_UNITS[unit]} diff --git a/tests/components/alexa/__init__.py b/tests/components/alexa/__init__.py index f853c4ef848..48406a11aef 100644 --- a/tests/components/alexa/__init__.py +++ b/tests/components/alexa/__init__.py @@ -171,6 +171,12 @@ class ReportedProperties: """Initialize class.""" self.properties = properties + def assert_not_has_property(self, namespace, name): + """Assert a property does not exist.""" + for prop in self.properties: + if prop["namespace"] == namespace and prop["name"] == name: + assert False, "Property %s:%s exists" + def assert_equal(self, namespace, name, value): """Assert a property is equal to a given value.""" for prop in self.properties: diff --git a/tests/components/alexa/test_capabilities.py b/tests/components/alexa/test_capabilities.py index f8ad3f57c42..357e0e3026d 100644 --- a/tests/components/alexa/test_capabilities.py +++ b/tests/components/alexa/test_capabilities.py @@ -1,7 +1,15 @@ """Test Alexa capabilities.""" import pytest -from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED, STATE_UNKNOWN +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + TEMP_CELSIUS, + STATE_LOCKED, + STATE_UNLOCKED, + STATE_UNKNOWN, + STATE_UNAVAILABLE, +) +from homeassistant.components import climate from homeassistant.components.alexa import smart_home from tests.common import async_mock_service @@ -368,3 +376,47 @@ async def test_report_cover_percentage_state(hass): properties = await reported_properties(hass, "cover.closed") properties.assert_equal("Alexa.PercentageController", "percentage", 0) + + +async def test_temperature_sensor_sensor(hass): + """Test TemperatureSensor reports sensor temperature correctly.""" + for bad_value in (STATE_UNKNOWN, STATE_UNAVAILABLE, "not-number"): + hass.states.async_set( + "sensor.temp_living_room", + bad_value, + {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}, + ) + + properties = await reported_properties(hass, "sensor.temp_living_room") + properties.assert_not_has_property("Alexa.TemperatureSensor", "temperature") + + hass.states.async_set( + "sensor.temp_living_room", "34", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} + ) + properties = await reported_properties(hass, "sensor.temp_living_room") + properties.assert_equal( + "Alexa.TemperatureSensor", "temperature", {"value": 34.0, "scale": "CELSIUS"} + ) + + +async def test_temperature_sensor_climate(hass): + """Test TemperatureSensor reports climate temperature correctly.""" + for bad_value in (STATE_UNKNOWN, STATE_UNAVAILABLE, "not-number"): + hass.states.async_set( + "climate.downstairs", + climate.HVAC_MODE_HEAT, + {climate.ATTR_CURRENT_TEMPERATURE: bad_value}, + ) + + properties = await reported_properties(hass, "climate.downstairs") + properties.assert_not_has_property("Alexa.TemperatureSensor", "temperature") + + hass.states.async_set( + "climate.downstairs", + climate.HVAC_MODE_HEAT, + {climate.ATTR_CURRENT_TEMPERATURE: 34}, + ) + properties = await reported_properties(hass, "climate.downstairs") + properties.assert_equal( + "Alexa.TemperatureSensor", "temperature", {"value": 34.0, "scale": "CELSIUS"} + )