2020-03-19 15:50:17 +00:00
|
|
|
"""Mocks for powerwall."""
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2020-04-13 19:59:50 +00:00
|
|
|
from asynctest import MagicMock, Mock
|
|
|
|
from tesla_powerwall import (
|
|
|
|
DeviceType,
|
|
|
|
GridStatus,
|
|
|
|
MetersAggregateResponse,
|
|
|
|
Powerwall,
|
|
|
|
PowerwallStatusResponse,
|
|
|
|
SiteInfoResponse,
|
|
|
|
SitemasterResponse,
|
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
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")
|
2020-03-31 19:55:50 +00:00
|
|
|
status = await _async_load_json_fixture(hass, "status.json")
|
|
|
|
device_type = await _async_load_json_fixture(hass, "device_type.json")
|
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
return _mock_powerwall_return_value(
|
2020-04-13 19:59:50 +00:00
|
|
|
site_info=SiteInfoResponse(site_info),
|
|
|
|
charge=47,
|
|
|
|
sitemaster=SitemasterResponse(sitemaster),
|
|
|
|
meters=MetersAggregateResponse(meters),
|
|
|
|
grid_status=GridStatus.CONNECTED,
|
|
|
|
status=PowerwallStatusResponse(status),
|
|
|
|
device_type=DeviceType(device_type["device_type"]),
|
2020-04-17 21:21:14 +00:00
|
|
|
serial_numbers=["TG0123456789AB", "TG9876543210BA"],
|
2020-03-19 15:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _mock_powerwall_return_value(
|
2020-03-31 19:55:50 +00:00
|
|
|
site_info=None,
|
|
|
|
charge=None,
|
|
|
|
sitemaster=None,
|
|
|
|
meters=None,
|
|
|
|
grid_status=None,
|
|
|
|
status=None,
|
|
|
|
device_type=None,
|
2020-04-17 21:21:14 +00:00
|
|
|
serial_numbers=None,
|
2020-03-19 15:50:17 +00:00
|
|
|
):
|
2020-04-13 19:59:50 +00:00
|
|
|
powerwall_mock = MagicMock(Powerwall("1.2.3.4"))
|
|
|
|
powerwall_mock.get_site_info = Mock(return_value=site_info)
|
|
|
|
powerwall_mock.get_charge = Mock(return_value=charge)
|
|
|
|
powerwall_mock.get_sitemaster = Mock(return_value=sitemaster)
|
|
|
|
powerwall_mock.get_meters = Mock(return_value=meters)
|
|
|
|
powerwall_mock.get_grid_status = Mock(return_value=grid_status)
|
|
|
|
powerwall_mock.get_status = Mock(return_value=status)
|
|
|
|
powerwall_mock.get_device_type = Mock(return_value=device_type)
|
2020-04-17 21:21:14 +00:00
|
|
|
powerwall_mock.get_serial_numbers = Mock(return_value=serial_numbers)
|
2020-04-13 19:59:50 +00:00
|
|
|
|
|
|
|
return powerwall_mock
|
|
|
|
|
|
|
|
|
|
|
|
async def _mock_powerwall_site_name(hass, site_name):
|
|
|
|
powerwall_mock = MagicMock(Powerwall("1.2.3.4"))
|
|
|
|
|
|
|
|
site_info_resp = SiteInfoResponse(
|
|
|
|
await _async_load_json_fixture(hass, "site_info.json")
|
|
|
|
)
|
|
|
|
site_info_resp.site_name = site_name
|
|
|
|
powerwall_mock.get_site_info = Mock(return_value=site_info_resp)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
return powerwall_mock
|
|
|
|
|
|
|
|
|
|
|
|
def _mock_powerwall_side_effect(site_info=None):
|
2020-04-13 19:59:50 +00:00
|
|
|
powerwall_mock = MagicMock(Powerwall("1.2.3.4"))
|
|
|
|
powerwall_mock.get_site_info = Mock(side_effect=site_info)
|
2020-03-19 15:50:17 +00:00
|
|
|
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"}}
|