From dfd33fdab1d1f1ed840a0e527bcd0b11c57fdbd8 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Sun, 5 Oct 2025 14:26:16 +0200 Subject: [PATCH] Fix sensors availability check for Alexa Devices (#153743) --- homeassistant/components/alexa_devices/binary_sensor.py | 4 +++- homeassistant/components/alexa_devices/sensor.py | 8 +++++--- homeassistant/components/alexa_devices/switch.py | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/alexa_devices/binary_sensor.py b/homeassistant/components/alexa_devices/binary_sensor.py index 010a561fa77e..8347fa34423a 100644 --- a/homeassistant/components/alexa_devices/binary_sensor.py +++ b/homeassistant/components/alexa_devices/binary_sensor.py @@ -51,7 +51,9 @@ BINARY_SENSORS: Final = ( ), is_supported=lambda device, key: device.sensors.get(key) is not None, is_available_fn=lambda device, key: ( - device.online and device.sensors[key].error is False + device.online + and (sensor := device.sensors.get(key)) is not None + and sensor.error is False ), ), ) diff --git a/homeassistant/components/alexa_devices/sensor.py b/homeassistant/components/alexa_devices/sensor.py index e6dbc251b950..57332b8ce3b1 100644 --- a/homeassistant/components/alexa_devices/sensor.py +++ b/homeassistant/components/alexa_devices/sensor.py @@ -32,7 +32,9 @@ class AmazonSensorEntityDescription(SensorEntityDescription): native_unit_of_measurement_fn: Callable[[AmazonDevice, str], str] | None = None is_available_fn: Callable[[AmazonDevice, str], bool] = lambda device, key: ( - device.online and device.sensors[key].error is False + device.online + and (sensor := device.sensors.get(key)) is not None + and sensor.error is False ) @@ -40,9 +42,9 @@ SENSORS: Final = ( AmazonSensorEntityDescription( key="temperature", device_class=SensorDeviceClass.TEMPERATURE, - native_unit_of_measurement_fn=lambda device, _key: ( + native_unit_of_measurement_fn=lambda device, key: ( UnitOfTemperature.CELSIUS - if device.sensors[_key].scale == "CELSIUS" + if key in device.sensors and device.sensors[key].scale == "CELSIUS" else UnitOfTemperature.FAHRENHEIT ), state_class=SensorStateClass.MEASUREMENT, diff --git a/homeassistant/components/alexa_devices/switch.py b/homeassistant/components/alexa_devices/switch.py index 2994ab777514..003f57620798 100644 --- a/homeassistant/components/alexa_devices/switch.py +++ b/homeassistant/components/alexa_devices/switch.py @@ -29,7 +29,9 @@ class AmazonSwitchEntityDescription(SwitchEntityDescription): is_on_fn: Callable[[AmazonDevice], bool] is_available_fn: Callable[[AmazonDevice, str], bool] = lambda device, key: ( - device.online and device.sensors[key].error is False + device.online + and (sensor := device.sensors.get(key)) is not None + and sensor.error is False ) method: str