mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Add to the Broadlink integration support for voltage, current, overload and total consumption sensors (#53628)
This commit is contained in:
parent
9bd2115a20
commit
6f0a7d2921
@ -6,16 +6,28 @@ import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
DEVICE_CLASS_CURRENT,
|
||||
DEVICE_CLASS_ENERGY,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
PLATFORM_SCHEMA,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, PERCENTAGE, POWER_WATT, TEMP_CELSIUS
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
ELECTRIC_CURRENT_AMPERE,
|
||||
ELECTRIC_POTENTIAL_VOLT,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
PERCENTAGE,
|
||||
POWER_WATT,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .const import DOMAIN
|
||||
@ -59,6 +71,34 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="volt",
|
||||
name="Voltage",
|
||||
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="current",
|
||||
name="Current",
|
||||
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="overload",
|
||||
name="Overload",
|
||||
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="totalconsum",
|
||||
name="Total consumption",
|
||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||
),
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
|
@ -58,6 +58,16 @@ BROADLINK_DEVICES = {
|
||||
20025,
|
||||
5,
|
||||
),
|
||||
"Dining room": (
|
||||
"192.168.0.16",
|
||||
"34ea34b4fd1c",
|
||||
"SCB1E",
|
||||
"Broadlink",
|
||||
"SP4B",
|
||||
0x5115,
|
||||
57,
|
||||
5,
|
||||
),
|
||||
"Kitchen": ( # Not supported.
|
||||
"192.168.0.64",
|
||||
"34ea34b61d2c",
|
||||
|
@ -1,10 +1,14 @@
|
||||
"""Tests for Broadlink sensors."""
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components.broadlink.const import DOMAIN, SENSOR_DOMAIN
|
||||
from homeassistant.components.broadlink.updater import BroadlinkSP4UpdateManager
|
||||
from homeassistant.helpers.entity_registry import async_entries_for_device
|
||||
from homeassistant.util import dt
|
||||
|
||||
from . import get_device
|
||||
|
||||
from tests.common import mock_device_registry, mock_registry
|
||||
from tests.common import async_fire_time_changed, mock_device_registry, mock_registry
|
||||
|
||||
|
||||
async def test_a1_sensor_setup(hass):
|
||||
@ -286,3 +290,107 @@ async def test_rm4_pro_no_sensor(hass):
|
||||
entries = async_entries_for_device(entity_registry, device_entry.id)
|
||||
sensors = {entry for entry in entries if entry.domain == SENSOR_DOMAIN}
|
||||
assert len(sensors) == 0
|
||||
|
||||
|
||||
async def test_scb1e_sensor_setup(hass):
|
||||
"""Test a successful SCB1E sensor setup."""
|
||||
device = get_device("Dining room")
|
||||
mock_api = device.get_mock_api()
|
||||
mock_api.get_state.return_value = {
|
||||
"pwr": 1,
|
||||
"indicator": 1,
|
||||
"maxworktime": 0,
|
||||
"power": 255.57,
|
||||
"volt": 121.7,
|
||||
"current": 2.1,
|
||||
"overload": 0,
|
||||
"totalconsum": 1.7,
|
||||
"childlock": 0,
|
||||
}
|
||||
|
||||
device_registry = mock_device_registry(hass)
|
||||
entity_registry = mock_registry(hass)
|
||||
|
||||
mock_setup = await device.setup_entry(hass, mock_api=mock_api)
|
||||
|
||||
assert mock_api.get_state.call_count == 1
|
||||
device_entry = device_registry.async_get_device(
|
||||
{(DOMAIN, mock_setup.entry.unique_id)}
|
||||
)
|
||||
entries = async_entries_for_device(entity_registry, device_entry.id)
|
||||
sensors = [entry for entry in entries if entry.domain == SENSOR_DOMAIN]
|
||||
assert len(sensors) == 5
|
||||
|
||||
sensors_and_states = {
|
||||
(sensor.original_name, hass.states.get(sensor.entity_id).state)
|
||||
for sensor in sensors
|
||||
}
|
||||
assert sensors_and_states == {
|
||||
(f"{device.name} Current power", "255.57"),
|
||||
(f"{device.name} Voltage", "121.7"),
|
||||
(f"{device.name} Current", "2.1"),
|
||||
(f"{device.name} Overload", "0"),
|
||||
(f"{device.name} Total consumption", "1.7"),
|
||||
}
|
||||
|
||||
|
||||
async def test_scb1e_sensor_update(hass):
|
||||
"""Test a successful SCB1E sensor update."""
|
||||
device = get_device("Dining room")
|
||||
mock_api = device.get_mock_api()
|
||||
mock_api.get_state.return_value = {
|
||||
"pwr": 1,
|
||||
"indicator": 1,
|
||||
"maxworktime": 0,
|
||||
"power": 255.6,
|
||||
"volt": 121.7,
|
||||
"current": 2.1,
|
||||
"overload": 0,
|
||||
"totalconsum": 1.7,
|
||||
"childlock": 0,
|
||||
}
|
||||
|
||||
device_registry = mock_device_registry(hass)
|
||||
entity_registry = mock_registry(hass)
|
||||
|
||||
target_time = (
|
||||
dt.utcnow() + BroadlinkSP4UpdateManager.SCAN_INTERVAL * 3 + timedelta(seconds=1)
|
||||
)
|
||||
|
||||
mock_setup = await device.setup_entry(hass, mock_api=mock_api)
|
||||
|
||||
device_entry = device_registry.async_get_device(
|
||||
{(DOMAIN, mock_setup.entry.unique_id)}
|
||||
)
|
||||
entries = async_entries_for_device(entity_registry, device_entry.id)
|
||||
sensors = [entry for entry in entries if entry.domain == SENSOR_DOMAIN]
|
||||
assert len(sensors) == 5
|
||||
|
||||
mock_setup.api.get_state.return_value = {
|
||||
"pwr": 1,
|
||||
"indicator": 1,
|
||||
"maxworktime": 0,
|
||||
"power": 291.8,
|
||||
"volt": 121.6,
|
||||
"current": 2.4,
|
||||
"overload": 0,
|
||||
"totalconsum": 0.5,
|
||||
"childlock": 0,
|
||||
}
|
||||
|
||||
async_fire_time_changed(hass, target_time)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_setup.api.get_state.call_count == 2
|
||||
|
||||
sensors_and_states = {
|
||||
(sensor.original_name, hass.states.get(sensor.entity_id).state)
|
||||
for sensor in sensors
|
||||
}
|
||||
assert sensors_and_states == {
|
||||
(f"{device.name} Current power", "291.8"),
|
||||
(f"{device.name} Voltage", "121.6"),
|
||||
(f"{device.name} Current", "2.4"),
|
||||
(f"{device.name} Overload", "0"),
|
||||
(f"{device.name} Total consumption", "0.5"),
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user