From fd38d9788d3885265013390c32709daec3e1ea49 Mon Sep 17 00:00:00 2001 From: Retha Runolfsson <137745329+zerzhang@users.noreply.github.com> Date: Thu, 5 Jun 2025 22:42:24 +0800 Subject: [PATCH] Bump pyswitchbot to 0.65.0 (#146133) * update pyswitchbot to 0.65.0 * fix relay switch 1pm test * fix ma to a --- .../components/switchbot/manifest.json | 2 +- homeassistant/components/switchbot/sensor.py | 9 +++- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/switchbot/test_sensor.py | 48 +++++++++++++++++-- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/switchbot/manifest.json b/homeassistant/components/switchbot/manifest.json index eadd3ad2a2d..a855b43f1e6 100644 --- a/homeassistant/components/switchbot/manifest.json +++ b/homeassistant/components/switchbot/manifest.json @@ -41,5 +41,5 @@ "iot_class": "local_push", "loggers": ["switchbot"], "quality_scale": "gold", - "requirements": ["PySwitchbot==0.64.1"] + "requirements": ["PySwitchbot==0.65.0"] } diff --git a/homeassistant/components/switchbot/sensor.py b/homeassistant/components/switchbot/sensor.py index 75ac0f7bc74..736297ca091 100644 --- a/homeassistant/components/switchbot/sensor.py +++ b/homeassistant/components/switchbot/sensor.py @@ -19,6 +19,7 @@ from homeassistant.const import ( EntityCategory, UnitOfElectricCurrent, UnitOfElectricPotential, + UnitOfEnergy, UnitOfPower, UnitOfTemperature, ) @@ -94,7 +95,7 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = { ), "current": SensorEntityDescription( key="current", - native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.CURRENT, ), @@ -110,6 +111,12 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = { device_class=SensorDeviceClass.ENUM, options=[member.name.lower() for member in AirQualityLevel], ), + "energy": SensorEntityDescription( + key="energy", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + state_class=SensorStateClass.TOTAL_INCREASING, + device_class=SensorDeviceClass.ENERGY, + ), } diff --git a/requirements_all.txt b/requirements_all.txt index 68caf7bff98..3c6dafe8078 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -81,7 +81,7 @@ PyQRCode==1.2.1 PyRMVtransport==0.3.3 # homeassistant.components.switchbot -PySwitchbot==0.64.1 +PySwitchbot==0.65.0 # homeassistant.components.switchmate PySwitchmate==0.5.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index e9fd61abe5e..544b7e78ee5 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -78,7 +78,7 @@ PyQRCode==1.2.1 PyRMVtransport==0.3.3 # homeassistant.components.switchbot -PySwitchbot==0.64.1 +PySwitchbot==0.65.0 # homeassistant.components.syncthru PySyncThru==0.8.0 diff --git a/tests/components/switchbot/test_sensor.py b/tests/components/switchbot/test_sensor.py index a04bff75c2d..db37f3f98dd 100644 --- a/tests/components/switchbot/test_sensor.py +++ b/tests/components/switchbot/test_sensor.py @@ -1,6 +1,6 @@ """Test the switchbot sensors.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, patch import pytest @@ -124,14 +124,21 @@ async def test_co2_sensor(hass: HomeAssistant) -> None: @pytest.mark.usefixtures("entity_registry_enabled_by_default") -async def test_relay_switch_1pm_power_sensor(hass: HomeAssistant) -> None: - """Test setting up creates the power sensor.""" +async def test_relay_switch_1pm_sensor(hass: HomeAssistant) -> None: + """Test setting up creates the relay switch 1PM sensor.""" await async_setup_component(hass, DOMAIN, {}) inject_bluetooth_service_info(hass, WORELAY_SWITCH_1PM_SERVICE_INFO) with patch( - "switchbot.SwitchbotRelaySwitch.update", - return_value=None, + "homeassistant.components.switchbot.switch.switchbot.SwitchbotRelaySwitch.get_basic_info", + new=AsyncMock( + return_value={ + "power": 4.9, + "current": 0.02, + "voltage": 25, + "energy": 0.2, + } + ), ): entry = MockConfigEntry( domain=DOMAIN, @@ -149,11 +156,42 @@ async def test_relay_switch_1pm_power_sensor(hass: HomeAssistant) -> None: assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + assert len(hass.states.async_all("sensor")) == 5 + power_sensor = hass.states.get("sensor.test_name_power") power_sensor_attrs = power_sensor.attributes assert power_sensor.state == "4.9" assert power_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Power" assert power_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "W" + assert power_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + voltage_sensor = hass.states.get("sensor.test_name_voltage") + voltage_sensor_attrs = voltage_sensor.attributes + assert voltage_sensor.state == "25" + assert voltage_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Voltage" + assert voltage_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "V" + assert voltage_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + current_sensor = hass.states.get("sensor.test_name_current") + current_sensor_attrs = current_sensor.attributes + assert current_sensor.state == "0.02" + assert current_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Current" + assert current_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "A" + assert current_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + energy_sensor = hass.states.get("sensor.test_name_energy") + energy_sensor_attrs = energy_sensor.attributes + assert energy_sensor.state == "0.2" + assert energy_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Energy" + assert energy_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "kWh" + assert energy_sensor_attrs[ATTR_STATE_CLASS] == "total_increasing" + + rssi_sensor = hass.states.get("sensor.test_name_bluetooth_signal") + rssi_sensor_attrs = rssi_sensor.attributes + assert rssi_sensor.state == "-60" + assert rssi_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Bluetooth signal" + assert rssi_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "dBm" + assert rssi_sensor_attrs[ATTR_STATE_CLASS] == "measurement" assert await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done()