Add Powerwalls integration (#32851)
* Create an integration for Powerwalls * Self review :: remove unused code * Remove debug * Update homeassistant/components/powerwall/__init__.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * _call_site_info to module level * Update homeassistant/components/powerwall/binary_sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/binary_sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/binary_sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/powerwall/test_binary_sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/binary_sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/binary_sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * remove sensors that I added tests for from the comment * Update homeassistant/components/powerwall/config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/powerwall/sensor.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Switch to UNIT_PERCENTAGE * reduce code * Add test for import * Adjust tests * Add missing file Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
242aff9269
commit
5b4d2aed64
20 files changed
+990
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for the Tesla Powerwall integration."""
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Mocks for powerwall."""
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
from asynctest import MagicMock, PropertyMock
|
||||
|
||||
from homeassistant.components.powerwall.const import DOMAIN
|
||||
from homeassistant.const import CONF_IP_ADDRESS
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
async def _mock_powerwall_with_fixtures(hass):
|
||||
"""Mock data used to build powerwall state."""
|
||||
meters = await _async_load_json_fixture(hass, "meters.json")
|
||||
sitemaster = await _async_load_json_fixture(hass, "sitemaster.json")
|
||||
site_info = await _async_load_json_fixture(hass, "site_info.json")
|
||||
return _mock_powerwall_return_value(
|
||||
site_info=site_info,
|
||||
charge=47.31993232,
|
||||
sitemaster=sitemaster,
|
||||
meters=meters,
|
||||
grid_status="SystemGridConnected",
|
||||
)
|
||||
|
||||
|
||||
def _mock_powerwall_return_value(
|
||||
site_info=None, charge=None, sitemaster=None, meters=None, grid_status=None
|
||||
):
|
||||
powerwall_mock = MagicMock()
|
||||
type(powerwall_mock).site_info = PropertyMock(return_value=site_info)
|
||||
type(powerwall_mock).charge = PropertyMock(return_value=charge)
|
||||
type(powerwall_mock).sitemaster = PropertyMock(return_value=sitemaster)
|
||||
type(powerwall_mock).meters = PropertyMock(return_value=meters)
|
||||
type(powerwall_mock).grid_status = PropertyMock(return_value=grid_status)
|
||||
|
||||
return powerwall_mock
|
||||
|
||||
|
||||
def _mock_powerwall_side_effect(site_info=None):
|
||||
powerwall_mock = MagicMock()
|
||||
type(powerwall_mock).site_info = PropertyMock(side_effect=site_info)
|
||||
return powerwall_mock
|
||||
|
||||
|
||||
async def _async_load_json_fixture(hass, path):
|
||||
fixture = await hass.async_add_executor_job(
|
||||
load_fixture, os.path.join("powerwall", path)
|
||||
)
|
||||
return json.loads(fixture)
|
||||
|
||||
|
||||
def _mock_get_config():
|
||||
"""Return a default powerwall config."""
|
||||
return {DOMAIN: {CONF_IP_ADDRESS: "1.2.3.4"}}
|
||||
@@ -0,0 +1,54 @@
|
||||
"""The binary sensor tests for the powerwall platform."""
|
||||
|
||||
from asynctest import patch
|
||||
|
||||
from homeassistant.components.powerwall.const import DOMAIN
|
||||
from homeassistant.const import STATE_ON
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .mocks import _mock_get_config, _mock_powerwall_with_fixtures
|
||||
|
||||
|
||||
async def test_sensors(hass):
|
||||
"""Test creation of the binary sensors."""
|
||||
|
||||
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.powerwall.config_flow.PowerWall",
|
||||
return_value=mock_powerwall,
|
||||
), patch(
|
||||
"homeassistant.components.powerwall.PowerWall", return_value=mock_powerwall,
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, _mock_get_config())
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.grid_status")
|
||||
assert state.state == STATE_ON
|
||||
expected_attributes = {"friendly_name": "Grid Status", "device_class": "power"}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
|
||||
state = hass.states.get("binary_sensor.powerwall_status")
|
||||
assert state.state == STATE_ON
|
||||
expected_attributes = {
|
||||
"region": "IEEE1547a:2014",
|
||||
"grid_code": "60Hz_240V_s_IEEE1547a_2014",
|
||||
"nominal_system_power_kW": 25,
|
||||
"friendly_name": "Powerwall Status",
|
||||
"device_class": "power",
|
||||
}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
|
||||
state = hass.states.get("binary_sensor.powerwall_connected_to_tesla")
|
||||
assert state.state == STATE_ON
|
||||
expected_attributes = {
|
||||
"friendly_name": "Powerwall Connected to Tesla",
|
||||
"device_class": "connectivity",
|
||||
}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Test the Powerwall config flow."""
|
||||
|
||||
from asynctest import patch
|
||||
from tesla_powerwall import PowerWallUnreachableError
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.powerwall.const import DOMAIN, POWERWALL_SITE_NAME
|
||||
from homeassistant.const import CONF_IP_ADDRESS
|
||||
|
||||
from .mocks import _mock_powerwall_return_value, _mock_powerwall_side_effect
|
||||
|
||||
|
||||
async def test_form_source_user(hass):
|
||||
"""Test we get config flow setup form as a user."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_powerwall = _mock_powerwall_return_value(
|
||||
site_info={POWERWALL_SITE_NAME: "My site"}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.powerwall.config_flow.PowerWall",
|
||||
return_value=mock_powerwall,
|
||||
), patch(
|
||||
"homeassistant.components.powerwall.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.powerwall.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_IP_ADDRESS: "1.2.3.4"},
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "My site"
|
||||
assert result2["data"] == {CONF_IP_ADDRESS: "1.2.3.4"}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_source_import(hass):
|
||||
"""Test we setup the config entry via import."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
mock_powerwall = _mock_powerwall_return_value(
|
||||
site_info={POWERWALL_SITE_NAME: "Imported site"}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.powerwall.config_flow.PowerWall",
|
||||
return_value=mock_powerwall,
|
||||
), patch(
|
||||
"homeassistant.components.powerwall.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.powerwall.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={CONF_IP_ADDRESS: "1.2.3.4"},
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "Imported site"
|
||||
assert result["data"] == {CONF_IP_ADDRESS: "1.2.3.4"}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_powerwall = _mock_powerwall_side_effect(site_info=PowerWallUnreachableError)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.powerwall.config_flow.PowerWall",
|
||||
return_value=mock_powerwall,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_IP_ADDRESS: "1.2.3.4"},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
@@ -0,0 +1,103 @@
|
||||
"""The sensor tests for the powerwall platform."""
|
||||
|
||||
from asynctest import patch
|
||||
|
||||
from homeassistant.components.powerwall.const import DOMAIN
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .mocks import _mock_get_config, _mock_powerwall_with_fixtures
|
||||
|
||||
|
||||
async def test_sensors(hass):
|
||||
"""Test creation of the sensors."""
|
||||
|
||||
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.powerwall.config_flow.PowerWall",
|
||||
return_value=mock_powerwall,
|
||||
), patch(
|
||||
"homeassistant.components.powerwall.PowerWall", return_value=mock_powerwall,
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, _mock_get_config())
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device_registry = await hass.helpers.device_registry.async_get_registry()
|
||||
reg_device = device_registry.async_get_device(
|
||||
identifiers={("powerwall", "Wom Energy_60Hz_240V_s_IEEE1547a_2014_25_13.5")},
|
||||
connections=set(),
|
||||
)
|
||||
assert reg_device.model == "PowerWall 2"
|
||||
assert reg_device.manufacturer == "Tesla"
|
||||
assert reg_device.name == "MySite"
|
||||
|
||||
state = hass.states.get("sensor.powerwall_site_now")
|
||||
assert state.state == "0.032"
|
||||
expected_attributes = {
|
||||
"frequency": 60,
|
||||
"energy_exported": 10429451.9916853,
|
||||
"energy_imported": 4824191.60668611,
|
||||
"instant_average_voltage": 120.650001525879,
|
||||
"unit_of_measurement": "kWh",
|
||||
"friendly_name": "Powerwall Site Now",
|
||||
"device_class": "power",
|
||||
}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
|
||||
state = hass.states.get("sensor.powerwall_load_now")
|
||||
assert state.state == "1.971"
|
||||
expected_attributes = {
|
||||
"frequency": 60,
|
||||
"energy_exported": 1056797.48917483,
|
||||
"energy_imported": 4692987.91889705,
|
||||
"instant_average_voltage": 120.650001525879,
|
||||
"unit_of_measurement": "kWh",
|
||||
"friendly_name": "Powerwall Load Now",
|
||||
"device_class": "power",
|
||||
}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
|
||||
state = hass.states.get("sensor.powerwall_battery_now")
|
||||
assert state.state == "-8.55"
|
||||
expected_attributes = {
|
||||
"frequency": 60.014,
|
||||
"energy_exported": 3620010,
|
||||
"energy_imported": 4216170,
|
||||
"instant_average_voltage": 240.56,
|
||||
"unit_of_measurement": "kWh",
|
||||
"friendly_name": "Powerwall Battery Now",
|
||||
"device_class": "power",
|
||||
}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
|
||||
state = hass.states.get("sensor.powerwall_solar_now")
|
||||
assert state.state == "10.49"
|
||||
expected_attributes = {
|
||||
"frequency": 60,
|
||||
"energy_exported": 9864205.82222448,
|
||||
"energy_imported": 28177.5358355867,
|
||||
"instant_average_voltage": 120.685001373291,
|
||||
"unit_of_measurement": "kWh",
|
||||
"friendly_name": "Powerwall Solar Now",
|
||||
"device_class": "power",
|
||||
}
|
||||
# Only test for a subset of attributes in case
|
||||
# HA changes the implementation and a new one appears
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
|
||||
state = hass.states.get("sensor.powerwall_charge")
|
||||
assert state.state == "47.32"
|
||||
expected_attributes = {
|
||||
"unit_of_measurement": "%",
|
||||
"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
|
||||
assert all(item in state.attributes.items() for item in expected_attributes.items())
|
||||
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"battery" : {
|
||||
"instant_power" : -8550,
|
||||
"i_b_current" : 0,
|
||||
"instant_average_voltage" : 240.56,
|
||||
"i_a_current" : 0,
|
||||
"frequency" : 60.014,
|
||||
"instant_reactive_power" : 50,
|
||||
"energy_imported" : 4216170,
|
||||
"instant_total_current" : 185.5,
|
||||
"timeout" : 1500000000,
|
||||
"energy_exported" : 3620010,
|
||||
"instant_apparent_power" : 8550.14619758048,
|
||||
"last_communication_time" : "2020-03-15T15:58:53.855997624-05:00",
|
||||
"i_c_current" : 0
|
||||
},
|
||||
"load" : {
|
||||
"i_b_current" : 0,
|
||||
"instant_average_voltage" : 120.650001525879,
|
||||
"instant_power" : 1971.46005249023,
|
||||
"instant_reactive_power" : -2119.58996582031,
|
||||
"i_a_current" : 0,
|
||||
"frequency" : 60,
|
||||
"last_communication_time" : "2020-03-15T15:58:53.853784964-05:00",
|
||||
"instant_apparent_power" : 2894.70488336392,
|
||||
"i_c_current" : 0,
|
||||
"instant_total_current" : 0,
|
||||
"energy_imported" : 4692987.91889705,
|
||||
"timeout" : 1500000000,
|
||||
"energy_exported" : 1056797.48917483
|
||||
},
|
||||
"solar" : {
|
||||
"i_a_current" : 0,
|
||||
"frequency" : 60,
|
||||
"instant_reactive_power" : -15.2600002288818,
|
||||
"instant_power" : 10489.6596679688,
|
||||
"i_b_current" : 0,
|
||||
"instant_average_voltage" : 120.685001373291,
|
||||
"timeout" : 1500000000,
|
||||
"energy_exported" : 9864205.82222448,
|
||||
"energy_imported" : 28177.5358355867,
|
||||
"instant_total_current" : 0,
|
||||
"i_c_current" : 0,
|
||||
"instant_apparent_power" : 10489.6707678276,
|
||||
"last_communication_time" : "2020-03-15T15:58:53.853898963-05:00"
|
||||
},
|
||||
"site" : {
|
||||
"instant_total_current" : 0.263575500367178,
|
||||
"energy_imported" : 4824191.60668611,
|
||||
"energy_exported" : 10429451.9916853,
|
||||
"timeout" : 1500000000,
|
||||
"last_communication_time" : "2020-03-15T15:58:53.853784964-05:00",
|
||||
"instant_apparent_power" : 2154.56465790676,
|
||||
"i_c_current" : 0,
|
||||
"instant_average_voltage" : 120.650001525879,
|
||||
"i_b_current" : 0,
|
||||
"instant_power" : 31.8003845214844,
|
||||
"instant_reactive_power" : -2154.32996559143,
|
||||
"i_a_current" : 0,
|
||||
"frequency" : 60
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"state" : "Somewhere",
|
||||
"utility" : "Wom Energy",
|
||||
"distributor" : "*",
|
||||
"max_system_energy_kWh" : 0,
|
||||
"nominal_system_power_kW" : 25,
|
||||
"grid_voltage_setting" : 240,
|
||||
"retailer" : "*",
|
||||
"grid_code" : "60Hz_240V_s_IEEE1547a_2014",
|
||||
"timezone" : "America/Chicago",
|
||||
"nominal_system_energy_kWh" : 13.5,
|
||||
"region" : "IEEE1547a:2014",
|
||||
"min_site_meter_power_kW" : -1000000000,
|
||||
"site_name" : "MySite",
|
||||
"country" : "United States",
|
||||
"max_site_meter_power_kW" : 1000000000,
|
||||
"grid_phase_setting" : "Split",
|
||||
"max_system_power_kW" : 0,
|
||||
"grid_freq_setting" : 60
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"connected_to_tesla": true, "running": true, "status": "StatusUp"}
|
||||
Reference in new issue
Block a user