mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Add energy sensor to bsblan (#163879)
This commit is contained in:
committed by
GitHub
parent
220e94d029
commit
4b53bc243d
@@ -29,8 +29,13 @@ if TYPE_CHECKING:
|
||||
|
||||
# Filter lists for optimized API calls - only fetch parameters we actually use
|
||||
# This significantly reduces response time (~0.2s per parameter saved)
|
||||
STATE_INCLUDE = ["current_temperature", "target_temperature", "hvac_mode"]
|
||||
SENSOR_INCLUDE = ["current_temperature", "outside_temperature"]
|
||||
STATE_INCLUDE = [
|
||||
"current_temperature",
|
||||
"target_temperature",
|
||||
"hvac_mode",
|
||||
"hvac_action",
|
||||
]
|
||||
SENSOR_INCLUDE = ["current_temperature", "outside_temperature", "total_energy"]
|
||||
DHW_STATE_INCLUDE = [
|
||||
"operating_mode",
|
||||
"nominal_setpoint",
|
||||
|
||||
@@ -11,7 +11,7 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import UnitOfTemperature
|
||||
from homeassistant.const import UnitOfEnergy, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
@@ -58,6 +58,19 @@ SENSOR_TYPES: tuple[BSBLanSensorEntityDescription, ...] = (
|
||||
),
|
||||
exists_fn=lambda data: data.sensor.outside_temperature is not None,
|
||||
),
|
||||
BSBLanSensorEntityDescription(
|
||||
key="total_energy",
|
||||
translation_key="total_energy",
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
value_fn=lambda data: (
|
||||
data.sensor.total_energy.value
|
||||
if data.sensor.total_energy is not None
|
||||
else None
|
||||
),
|
||||
exists_fn=lambda data: data.sensor.total_energy is not None,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -66,6 +66,9 @@
|
||||
},
|
||||
"outside_temperature": {
|
||||
"name": "Outside temperature"
|
||||
},
|
||||
"total_energy": {
|
||||
"name": "Total energy"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -16,5 +16,14 @@
|
||||
"dataType": 0,
|
||||
"readonly": 1,
|
||||
"unit": "°C"
|
||||
},
|
||||
"total_energy": {
|
||||
"name": "Total energy",
|
||||
"error": 0,
|
||||
"value": "7968",
|
||||
"desc": "",
|
||||
"dataType": 0,
|
||||
"readonly": 1,
|
||||
"unit": "kWh"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,19 @@
|
||||
'unit': '°C',
|
||||
'value': 6.1,
|
||||
}),
|
||||
'total_energy': None,
|
||||
'total_energy': dict({
|
||||
'data_type': 0,
|
||||
'data_type_family': '',
|
||||
'data_type_name': '',
|
||||
'desc': '',
|
||||
'error': 0,
|
||||
'name': 'Total energy',
|
||||
'precision': None,
|
||||
'readonly': 1,
|
||||
'readwrite': 0,
|
||||
'unit': 'kWh',
|
||||
'value': 7968,
|
||||
}),
|
||||
}),
|
||||
'state': dict({
|
||||
'current_temperature': dict({
|
||||
|
||||
@@ -113,3 +113,60 @@
|
||||
'state': '6.1',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_entity_properties[sensor.bsb_lan_total_energy-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.bsb_lan_total_energy',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Total energy',
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 2,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Total energy',
|
||||
'platform': 'bsblan',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'total_energy',
|
||||
'unique_id': '00:80:41:19:69:90-total_energy',
|
||||
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_entity_properties[sensor.bsb_lan_total_energy-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'energy',
|
||||
'friendly_name': 'BSB-LAN Total energy',
|
||||
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
|
||||
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.bsb_lan_total_energy',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '7968',
|
||||
})
|
||||
# ---
|
||||
|
||||
@@ -15,6 +15,7 @@ from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
ENTITY_CURRENT_TEMP = "sensor.bsb_lan_current_temperature"
|
||||
ENTITY_OUTSIDE_TEMP = "sensor.bsb_lan_outside_temperature"
|
||||
ENTITY_TOTAL_ENERGY = "sensor.bsb_lan_total_energy"
|
||||
|
||||
|
||||
async def test_sensor_entity_properties(
|
||||
@@ -40,6 +41,7 @@ async def test_sensors_not_created_when_data_unavailable(
|
||||
# Set all sensor data to None to simulate no sensors available
|
||||
mock_bsblan.sensor.return_value.current_temperature = None
|
||||
mock_bsblan.sensor.return_value.outside_temperature = None
|
||||
mock_bsblan.sensor.return_value.total_energy = None
|
||||
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
|
||||
|
||||
@@ -58,8 +60,9 @@ async def test_partial_sensors_created_when_some_data_available(
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test only available sensors are created when some sensor data is available."""
|
||||
# Only current temperature available, outside temperature not
|
||||
# Only current temperature available, outside temperature and energy not
|
||||
mock_bsblan.sensor.return_value.outside_temperature = None
|
||||
mock_bsblan.sensor.return_value.total_energy = None
|
||||
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user