From 52067dbfe512fee3e6e8b410d5be348f18c51cdf Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Thu, 12 Oct 2023 13:01:08 +0200 Subject: [PATCH] Fix mysensors battery level attribute (#101868) --- homeassistant/components/mysensors/device.py | 4 +++- tests/components/mysensors/test_binary_sensor.py | 3 ++- tests/components/mysensors/test_climate.py | 5 ++++- tests/components/mysensors/test_cover.py | 3 ++- tests/components/mysensors/test_device_tracker.py | 8 +++++++- tests/components/mysensors/test_light.py | 4 ++++ tests/components/mysensors/test_remote.py | 8 +++++++- tests/components/mysensors/test_sensor.py | 10 ++++++++++ tests/components/mysensors/test_switch.py | 2 ++ tests/components/mysensors/test_text.py | 3 ++- 10 files changed, 43 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/mysensors/device.py b/homeassistant/components/mysensors/device.py index 9e1d91c7cce..6d7decf14f4 100644 --- a/homeassistant/components/mysensors/device.py +++ b/homeassistant/components/mysensors/device.py @@ -8,7 +8,7 @@ from typing import Any from mysensors import BaseAsyncGateway, Sensor from mysensors.sensor import ChildSensor -from homeassistant.const import STATE_OFF, STATE_ON, Platform +from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_OFF, STATE_ON, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.device_registry import DeviceInfo @@ -212,6 +212,8 @@ class MySensorsChildEntity(MySensorNodeEntity): attr[ATTR_CHILD_ID] = self.child_id attr[ATTR_DESCRIPTION] = self._child.description + # We should deprecate the battery level attribute in the future. + attr[ATTR_BATTERY_LEVEL] = self._node.battery_level set_req = self.gateway.const.SetReq for value_type, value in self._values.items(): diff --git a/tests/components/mysensors/test_binary_sensor.py b/tests/components/mysensors/test_binary_sensor.py index 886c13e6ff5..a6dce9c78b9 100644 --- a/tests/components/mysensors/test_binary_sensor.py +++ b/tests/components/mysensors/test_binary_sensor.py @@ -6,7 +6,7 @@ from collections.abc import Callable from mysensors.sensor import Sensor from homeassistant.components.binary_sensor import BinarySensorDeviceClass -from homeassistant.const import ATTR_DEVICE_CLASS +from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_DEVICE_CLASS from homeassistant.core import HomeAssistant @@ -23,6 +23,7 @@ async def test_door_sensor( assert state assert state.state == "off" assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.DOOR + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 receive_message("1;1;1;0;16;1\n") await hass.async_block_till_done() diff --git a/tests/components/mysensors/test_climate.py b/tests/components/mysensors/test_climate.py index 730960f118d..6c386af6fd6 100644 --- a/tests/components/mysensors/test_climate.py +++ b/tests/components/mysensors/test_climate.py @@ -19,7 +19,7 @@ from homeassistant.components.climate import ( SERVICE_SET_TEMPERATURE, HVACMode, ) -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_ENTITY_ID from homeassistant.core import HomeAssistant @@ -36,6 +36,7 @@ async def test_hvac_node_auto( assert state assert state.state == HVACMode.OFF + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test set hvac mode auto await hass.services.async_call( @@ -150,6 +151,7 @@ async def test_hvac_node_heat( assert state assert state.state == HVACMode.OFF + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test set hvac mode heat await hass.services.async_call( @@ -259,6 +261,7 @@ async def test_hvac_node_cool( assert state assert state.state == HVACMode.OFF + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test set hvac mode heat await hass.services.async_call( diff --git a/tests/components/mysensors/test_cover.py b/tests/components/mysensors/test_cover.py index 494800f388f..7d0a098fc0a 100644 --- a/tests/components/mysensors/test_cover.py +++ b/tests/components/mysensors/test_cover.py @@ -19,7 +19,7 @@ from homeassistant.components.cover import ( STATE_OPEN, STATE_OPENING, ) -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_ENTITY_ID from homeassistant.core import HomeAssistant @@ -37,6 +37,7 @@ async def test_cover_node_percentage( assert state assert state.state == STATE_CLOSED assert state.attributes[ATTR_CURRENT_POSITION] == 0 + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 await hass.services.async_call( COVER_DOMAIN, diff --git a/tests/components/mysensors/test_device_tracker.py b/tests/components/mysensors/test_device_tracker.py index 63c9ed7b1da..4d6e638e665 100644 --- a/tests/components/mysensors/test_device_tracker.py +++ b/tests/components/mysensors/test_device_tracker.py @@ -6,7 +6,12 @@ from collections.abc import Callable from mysensors.sensor import Sensor from homeassistant.components.device_tracker import ATTR_SOURCE_TYPE, SourceType -from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_NOT_HOME +from homeassistant.const import ( + ATTR_BATTERY_LEVEL, + ATTR_LATITUDE, + ATTR_LONGITUDE, + STATE_NOT_HOME, +) from homeassistant.core import HomeAssistant @@ -32,6 +37,7 @@ async def test_gps_sensor( assert state.attributes[ATTR_SOURCE_TYPE] == SourceType.GPS assert state.attributes[ATTR_LATITUDE] == float(latitude) assert state.attributes[ATTR_LONGITUDE] == float(longitude) + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 latitude = "40.782" longitude = "-73.965" diff --git a/tests/components/mysensors/test_light.py b/tests/components/mysensors/test_light.py index 8d4ce445779..9696c6e622a 100644 --- a/tests/components/mysensors/test_light.py +++ b/tests/components/mysensors/test_light.py @@ -12,6 +12,7 @@ from homeassistant.components.light import ( ATTR_RGBW_COLOR, DOMAIN as LIGHT_DOMAIN, ) +from homeassistant.const import ATTR_BATTERY_LEVEL from homeassistant.core import HomeAssistant @@ -28,6 +29,7 @@ async def test_dimmer_node( assert state assert state.state == "off" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test turn on await hass.services.async_call( @@ -108,6 +110,7 @@ async def test_rgb_node( assert state assert state.state == "off" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test turn on await hass.services.async_call( @@ -218,6 +221,7 @@ async def test_rgbw_node( assert state assert state.state == "off" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test turn on await hass.services.async_call( diff --git a/tests/components/mysensors/test_remote.py b/tests/components/mysensors/test_remote.py index adc8590914c..586e2e2d048 100644 --- a/tests/components/mysensors/test_remote.py +++ b/tests/components/mysensors/test_remote.py @@ -14,7 +14,12 @@ from homeassistant.components.remote import ( SERVICE_LEARN_COMMAND, SERVICE_SEND_COMMAND, ) -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.const import ( + ATTR_BATTERY_LEVEL, + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) from homeassistant.core import HomeAssistant @@ -31,6 +36,7 @@ async def test_ir_transceiver( assert state assert state.state == "off" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 # Test turn on await hass.services.async_call( diff --git a/tests/components/mysensors/test_sensor.py b/tests/components/mysensors/test_sensor.py index 17301e4b212..d80fddea9e3 100644 --- a/tests/components/mysensors/test_sensor.py +++ b/tests/components/mysensors/test_sensor.py @@ -12,6 +12,7 @@ from homeassistant.components.sensor import ( SensorStateClass, ) from homeassistant.const import ( + ATTR_BATTERY_LEVEL, ATTR_DEVICE_CLASS, ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, @@ -41,6 +42,7 @@ async def test_gps_sensor( assert state assert state.state == "40.741894,-73.989311,12" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 altitude = 0 new_coords = "40.782,-73.965" @@ -67,6 +69,7 @@ async def test_ir_transceiver( assert state assert state.state == "test_code" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 receive_message("1;1;1;0;50;new_code\n") await hass.async_block_till_done() @@ -87,6 +90,7 @@ async def test_battery_entity( state = hass.states.get(battery_entity_id) assert state assert state.state == "42" + assert ATTR_BATTERY_LEVEL not in state.attributes receive_message("1;255;3;0;0;84\n") await hass.async_block_till_done() @@ -94,6 +98,7 @@ async def test_battery_entity( state = hass.states.get(battery_entity_id) assert state assert state.state == "84" + assert ATTR_BATTERY_LEVEL not in state.attributes async def test_power_sensor( @@ -111,6 +116,7 @@ async def test_power_sensor( assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.POWER assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfPower.WATT assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 async def test_energy_sensor( @@ -128,6 +134,7 @@ async def test_energy_sensor( assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL_INCREASING + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 async def test_sound_sensor( @@ -144,6 +151,7 @@ async def test_sound_sensor( assert state.state == "10" assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.SOUND_PRESSURE assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "dB" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 async def test_distance_sensor( @@ -161,6 +169,7 @@ async def test_distance_sensor( assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.DISTANCE assert ATTR_ICON not in state.attributes assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "cm" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 @pytest.mark.parametrize( @@ -193,3 +202,4 @@ async def test_temperature_sensor( assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == unit assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 diff --git a/tests/components/mysensors/test_switch.py b/tests/components/mysensors/test_switch.py index 59cea514d77..49786768ff7 100644 --- a/tests/components/mysensors/test_switch.py +++ b/tests/components/mysensors/test_switch.py @@ -7,6 +7,7 @@ from unittest.mock import MagicMock, call from mysensors.sensor import Sensor from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import ATTR_BATTERY_LEVEL from homeassistant.core import HomeAssistant @@ -23,6 +24,7 @@ async def test_relay_node( assert state assert state.state == "off" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 await hass.services.async_call( SWITCH_DOMAIN, diff --git a/tests/components/mysensors/test_text.py b/tests/components/mysensors/test_text.py index 7ed46532c8a..7490cfddfbf 100644 --- a/tests/components/mysensors/test_text.py +++ b/tests/components/mysensors/test_text.py @@ -12,7 +12,7 @@ from homeassistant.components.text import ( DOMAIN as TEXT_DOMAIN, SERVICE_SET_VALUE, ) -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_ENTITY_ID from homeassistant.core import HomeAssistant @@ -29,6 +29,7 @@ async def test_text_node( assert state assert state.state == "test" + assert state.attributes[ATTR_BATTERY_LEVEL] == 0 await hass.services.async_call( TEXT_DOMAIN,