2020-03-19 15:50:17 +00:00
|
|
|
"""The sensor tests for the powerwall platform."""
|
2022-04-08 03:34:00 +00:00
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
|
|
from tesla_powerwall.error import MissingAttributeError
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
from homeassistant.components.powerwall.const import DOMAIN
|
2022-03-18 20:23:26 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
CONF_IP_ADDRESS,
|
|
|
|
PERCENTAGE,
|
|
|
|
)
|
2021-03-09 13:28:32 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2021-01-22 02:59:33 +00:00
|
|
|
from .mocks import _mock_powerwall_with_fixtures
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
|
2022-03-18 20:23:26 +00:00
|
|
|
async def test_sensors(hass, entity_registry_enabled_by_default):
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Test creation of the sensors."""
|
|
|
|
|
|
|
|
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
|
|
|
|
2021-01-22 02:59:33 +00:00
|
|
|
config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_IP_ADDRESS: "1.2.3.4"})
|
|
|
|
config_entry.add_to_hass(hass)
|
2020-03-19 15:50:17 +00:00
|
|
|
with patch(
|
2020-04-13 19:59:50 +00:00
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
2020-03-19 15:50:17 +00:00
|
|
|
return_value=mock_powerwall,
|
|
|
|
), patch(
|
2020-04-13 19:59:50 +00:00
|
|
|
"homeassistant.components.powerwall.Powerwall", return_value=mock_powerwall
|
2020-03-19 15:50:17 +00:00
|
|
|
):
|
2021-01-22 02:59:33 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
2020-03-19 15:50:17 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-03-09 13:28:32 +00:00
|
|
|
device_registry = dr.async_get(hass)
|
2020-03-19 15:50:17 +00:00
|
|
|
reg_device = device_registry.async_get_device(
|
2020-08-27 11:56:20 +00:00
|
|
|
identifiers={("powerwall", "TG0123456789AB_TG9876543210BA")},
|
2020-03-19 15:50:17 +00:00
|
|
|
)
|
2020-04-13 19:59:50 +00:00
|
|
|
assert reg_device.model == "PowerWall 2 (GW1)"
|
2020-03-31 19:55:50 +00:00
|
|
|
assert reg_device.sw_version == "1.45.1"
|
2020-03-19 15:50:17 +00:00
|
|
|
assert reg_device.manufacturer == "Tesla"
|
|
|
|
assert reg_device.name == "MySite"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.powerwall_load_now")
|
|
|
|
assert state.state == "1.971"
|
2022-03-18 20:23:26 +00:00
|
|
|
attributes = state.attributes
|
|
|
|
assert attributes[ATTR_DEVICE_CLASS] == "power"
|
|
|
|
assert attributes[ATTR_UNIT_OF_MEASUREMENT] == "kW"
|
|
|
|
assert attributes[ATTR_STATE_CLASS] == "measurement"
|
|
|
|
assert attributes[ATTR_FRIENDLY_NAME] == "Powerwall Load Now"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.powerwall_load_frequency_now")
|
|
|
|
assert state.state == "60"
|
|
|
|
attributes = state.attributes
|
|
|
|
assert attributes[ATTR_DEVICE_CLASS] == "frequency"
|
|
|
|
assert attributes[ATTR_UNIT_OF_MEASUREMENT] == "Hz"
|
|
|
|
assert attributes[ATTR_STATE_CLASS] == "measurement"
|
|
|
|
assert attributes[ATTR_FRIENDLY_NAME] == "Powerwall Load Frequency Now"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.powerwall_load_average_voltage_now")
|
|
|
|
assert state.state == "120.7"
|
|
|
|
attributes = state.attributes
|
|
|
|
assert attributes[ATTR_DEVICE_CLASS] == "voltage"
|
|
|
|
assert attributes[ATTR_UNIT_OF_MEASUREMENT] == "V"
|
|
|
|
assert attributes[ATTR_STATE_CLASS] == "measurement"
|
|
|
|
assert attributes[ATTR_FRIENDLY_NAME] == "Powerwall Load Average Voltage Now"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.powerwall_load_average_current_now")
|
|
|
|
assert state.state == "0"
|
|
|
|
attributes = state.attributes
|
|
|
|
assert attributes[ATTR_DEVICE_CLASS] == "current"
|
|
|
|
assert attributes[ATTR_UNIT_OF_MEASUREMENT] == "A"
|
|
|
|
assert attributes[ATTR_STATE_CLASS] == "measurement"
|
|
|
|
assert attributes[ATTR_FRIENDLY_NAME] == "Powerwall Load Average Current Now"
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2021-08-09 23:03:22 +00:00
|
|
|
assert float(hass.states.get("sensor.powerwall_load_export").state) == 1056.8
|
|
|
|
assert float(hass.states.get("sensor.powerwall_load_import").state) == 4693.0
|
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
state = hass.states.get("sensor.powerwall_battery_now")
|
|
|
|
assert state.state == "-8.55"
|
|
|
|
|
2021-08-09 23:03:22 +00:00
|
|
|
assert float(hass.states.get("sensor.powerwall_battery_export").state) == 3620.0
|
|
|
|
assert float(hass.states.get("sensor.powerwall_battery_import").state) == 4216.2
|
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
state = hass.states.get("sensor.powerwall_solar_now")
|
|
|
|
assert state.state == "10.49"
|
|
|
|
|
2021-08-09 23:03:22 +00:00
|
|
|
assert float(hass.states.get("sensor.powerwall_solar_export").state) == 9864.2
|
|
|
|
assert float(hass.states.get("sensor.powerwall_solar_import").state) == 28.2
|
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
state = hass.states.get("sensor.powerwall_charge")
|
2020-04-13 19:59:50 +00:00
|
|
|
assert state.state == "47"
|
2020-03-19 15:50:17 +00:00
|
|
|
expected_attributes = {
|
2020-09-05 19:09:14 +00:00
|
|
|
"unit_of_measurement": PERCENTAGE,
|
2020-03-19 15:50:17 +00:00
|
|
|
"friendly_name": "Powerwall Charge",
|
|
|
|
"device_class": "battery",
|
|
|
|
}
|
|
|
|
# Only test for a subset of attributes in case
|
|
|
|
# HA changes the implementation and a new one appears
|
2020-04-10 16:33:58 +00:00
|
|
|
for key, value in expected_attributes.items():
|
|
|
|
assert state.attributes[key] == value
|
2022-02-28 19:31:34 +00:00
|
|
|
|
|
|
|
state = hass.states.get("sensor.powerwall_backup_reserve")
|
|
|
|
assert state.state == "15"
|
|
|
|
expected_attributes = {
|
|
|
|
"unit_of_measurement": PERCENTAGE,
|
|
|
|
"friendly_name": "Powerwall Backup Reserve",
|
|
|
|
"device_class": "battery",
|
|
|
|
}
|
|
|
|
# Only test for a subset of attributes in case
|
|
|
|
# HA changes the implementation and a new one appears
|
|
|
|
for key, value in expected_attributes.items():
|
|
|
|
assert state.attributes[key] == value
|
2022-04-08 03:34:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_sensor_backup_reserve_unavailable(hass):
|
|
|
|
"""Confirm that backup reserve sensor is not added if data is unavailable from the device."""
|
|
|
|
|
|
|
|
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
|
|
|
mock_powerwall.get_backup_reserve_percentage = Mock(
|
|
|
|
side_effect=MissingAttributeError(Mock(), "backup_reserve_percent", "operation")
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_IP_ADDRESS: "1.2.3.4"})
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.powerwall.Powerwall", return_value=mock_powerwall
|
|
|
|
):
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.powerwall_backup_reserve")
|
|
|
|
assert state is None
|