Skip to content

Commit

Permalink
Update the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas committed Aug 14, 2022
1 parent 996af1f commit a1b84f3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 29 deletions.
4 changes: 3 additions & 1 deletion homeassistant/components/p1_monitor/__init__.py
Expand Up @@ -3,7 +3,7 @@

from typing import TypedDict

from p1monitor import P1Monitor, Phases, Settings, SmartMeter
from p1monitor import P1Monitor, Phases, Settings, SmartMeter, WaterMeter

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, Platform
Expand Down Expand Up @@ -57,6 +57,7 @@ class P1MonitorData(TypedDict):
smartmeter: SmartMeter
phases: Phases
settings: Settings
watermeter: WaterMeter | None


class P1MonitorDataUpdateCoordinator(DataUpdateCoordinator[P1MonitorData]):
Expand Down Expand Up @@ -86,6 +87,7 @@ async def _async_update_data(self) -> P1MonitorData:
SERVICE_SMARTMETER: await self.p1monitor.smartmeter(),
SERVICE_PHASES: await self.p1monitor.phases(),
SERVICE_SETTINGS: await self.p1monitor.settings(),
SERVICE_WATERMETER: None,
}
if self.config_entry.data[CONF_WATERMETER]:
data[SERVICE_WATERMETER] = await self.p1monitor.watermeter()
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/p1_monitor/sensor.py
Expand Up @@ -221,11 +221,13 @@
SensorEntityDescription(
key="consumption_day",
name="Consumption Day",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=VOLUME_LITERS,
),
SensorEntityDescription(
key="consumption_total",
name="Consumption Total",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=VOLUME_CUBIC_METERS,
),
SensorEntityDescription(
Expand Down
11 changes: 8 additions & 3 deletions tests/components/p1_monitor/conftest.py
Expand Up @@ -2,10 +2,10 @@
import json
from unittest.mock import AsyncMock, MagicMock, patch

from p1monitor import Phases, Settings, SmartMeter
from p1monitor import Phases, Settings, SmartMeter, WaterMeter
import pytest

from homeassistant.components.p1_monitor.const import DOMAIN
from homeassistant.components.p1_monitor.const import CONF_WATERMETER, DOMAIN
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant

Expand All @@ -18,7 +18,7 @@ def mock_config_entry() -> MockConfigEntry:
return MockConfigEntry(
title="monitor",
domain=DOMAIN,
data={CONF_HOST: "example"},
data={CONF_HOST: "example", CONF_WATERMETER: True},
unique_id="unique_thingy",
)

Expand All @@ -43,6 +43,11 @@ def mock_p1monitor():
json.loads(load_fixture("p1_monitor/settings.json"))
)
)
client.watermeter = AsyncMock(
return_value=WaterMeter.from_dict(
json.loads(load_fixture("p1_monitor/watermeter.json"))
)
)
yield p1monitor_mock


Expand Down
10 changes: 10 additions & 0 deletions tests/components/p1_monitor/fixtures/watermeter.json
@@ -0,0 +1,10 @@
[
{
"TIMEPERIOD_ID": 13,
"TIMESTAMP_UTC": 1656194400,
"TIMESTAMP_lOCAL": "2022-06-26 00:00:00",
"WATERMETER_CONSUMPTION_LITER": 112.0,
"WATERMETER_CONSUMPTION_TOTAL_M3": 1696.14,
"WATERMETER_PULS_COUNT": 112.0
}
]
12 changes: 4 additions & 8 deletions tests/components/p1_monitor/test_config_flow.py
Expand Up @@ -3,7 +3,7 @@

from p1monitor import P1MonitorError

from homeassistant.components.p1_monitor.const import DOMAIN
from homeassistant.components.p1_monitor.const import CONF_WATERMETER, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
Expand All @@ -30,14 +30,13 @@ async def test_full_user_flow(hass: HomeAssistant) -> None:
user_input={
CONF_NAME: "Name",
CONF_HOST: "example.com",
CONF_WATERMETER: True,
},
)

assert result2.get("type") == FlowResultType.CREATE_ENTRY
assert result2.get("title") == "Name"
assert result2.get("data") == {
CONF_HOST: "example.com",
}
assert result2.get("data") == {CONF_HOST: "example.com", CONF_WATERMETER: True}

assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_p1monitor.mock_calls) == 1
Expand All @@ -52,10 +51,7 @@ async def test_api_error(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_NAME: "Name",
CONF_HOST: "example.com",
},
data={CONF_NAME: "Name", CONF_HOST: "example.com", CONF_WATERMETER: False},
)

assert result.get("type") == FlowResultType.FORM
Expand Down
1 change: 1 addition & 0 deletions tests/components/p1_monitor/test_diagnostics.py
Expand Up @@ -21,6 +21,7 @@ async def test_diagnostics(
"title": "monitor",
"data": {
"host": REDACTED,
"watermeter": True,
},
},
"data": {
Expand Down
64 changes: 47 additions & 17 deletions tests/components/p1_monitor/test_sensor.py
Expand Up @@ -17,6 +17,7 @@
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
VOLUME_LITERS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
Expand All @@ -33,8 +34,8 @@ async def test_smartmeter(
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)

state = hass.states.get("sensor.monitor_power_consumption")
entry = entity_registry.async_get("sensor.monitor_power_consumption")
state = hass.states.get("sensor.smartmeter_power_consumption")
entry = entity_registry.async_get("sensor.smartmeter_power_consumption")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_smartmeter_power_consumption"
Expand All @@ -45,8 +46,8 @@ async def test_smartmeter(
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert ATTR_ICON not in state.attributes

state = hass.states.get("sensor.monitor_energy_consumption_high")
entry = entity_registry.async_get("sensor.monitor_energy_consumption_high")
state = hass.states.get("sensor.smartmeter_energy_consumption_high")
entry = entity_registry.async_get("sensor.smartmeter_energy_consumption_high")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_smartmeter_energy_consumption_high"
Expand All @@ -59,8 +60,8 @@ async def test_smartmeter(
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
assert ATTR_ICON not in state.attributes

state = hass.states.get("sensor.monitor_energy_tariff_period")
entry = entity_registry.async_get("sensor.monitor_energy_tariff_period")
state = hass.states.get("sensor.smartmeter_energy_tariff_period")
entry = entity_registry.async_get("sensor.smartmeter_energy_tariff_period")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_smartmeter_energy_tariff_period"
Expand Down Expand Up @@ -90,8 +91,8 @@ async def test_phases(
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)

state = hass.states.get("sensor.monitor_voltage_phase_l1")
entry = entity_registry.async_get("sensor.monitor_voltage_phase_l1")
state = hass.states.get("sensor.phases_voltage_phase_l1")
entry = entity_registry.async_get("sensor.phases_voltage_phase_l1")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_phases_voltage_phase_l1"
Expand All @@ -102,8 +103,8 @@ async def test_phases(
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.VOLTAGE
assert ATTR_ICON not in state.attributes

state = hass.states.get("sensor.monitor_current_phase_l1")
entry = entity_registry.async_get("sensor.monitor_current_phase_l1")
state = hass.states.get("sensor.phases_current_phase_l1")
entry = entity_registry.async_get("sensor.phases_current_phase_l1")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_phases_current_phase_l1"
Expand All @@ -114,8 +115,8 @@ async def test_phases(
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.CURRENT
assert ATTR_ICON not in state.attributes

state = hass.states.get("sensor.monitor_power_consumed_phase_l1")
entry = entity_registry.async_get("sensor.monitor_power_consumed_phase_l1")
state = hass.states.get("sensor.phases_power_consumed_phase_l1")
entry = entity_registry.async_get("sensor.phases_power_consumed_phase_l1")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_phases_power_consumed_phase_l1"
Expand Down Expand Up @@ -146,8 +147,8 @@ async def test_settings(
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)

state = hass.states.get("sensor.monitor_energy_consumption_price_low")
entry = entity_registry.async_get("sensor.monitor_energy_consumption_price_low")
state = hass.states.get("sensor.settings_energy_consumption_price_low")
entry = entity_registry.async_get("sensor.settings_energy_consumption_price_low")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_settings_energy_consumption_price_low"
Expand All @@ -159,8 +160,8 @@ async def test_settings(
== f"{CURRENCY_EURO}/{ENERGY_KILO_WATT_HOUR}"
)

state = hass.states.get("sensor.monitor_energy_production_price_low")
entry = entity_registry.async_get("sensor.monitor_energy_production_price_low")
state = hass.states.get("sensor.settings_energy_production_price_low")
entry = entity_registry.async_get("sensor.settings_energy_production_price_low")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_settings_energy_production_price_low"
Expand All @@ -183,9 +184,38 @@ async def test_settings(
assert not device_entry.sw_version


async def test_watermeter(
hass: HomeAssistant,
init_integration: MockConfigEntry,
) -> None:
"""Test the P1 Monitor - WaterMeter sensors."""
entry_id = init_integration.entry_id
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)
state = hass.states.get("sensor.watermeter_consumption_day")
entry = entity_registry.async_get("sensor.watermeter_consumption_day")
assert entry
assert state
assert entry.unique_id == f"{entry_id}_watermeter_consumption_day"
assert state.state == "112.0"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Consumption Day"
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_LITERS

assert entry.device_id
device_entry = device_registry.async_get(entry.device_id)
assert device_entry
assert device_entry.identifiers == {(DOMAIN, f"{entry_id}_watermeter")}
assert device_entry.manufacturer == "P1 Monitor"
assert device_entry.name == "WaterMeter"
assert device_entry.entry_type is dr.DeviceEntryType.SERVICE
assert not device_entry.model
assert not device_entry.sw_version


@pytest.mark.parametrize(
"entity_id",
("sensor.monitor_gas_consumption",),
("sensor.smartmeter_gas_consumption",),
)
async def test_smartmeter_disabled_by_default(
hass: HomeAssistant, init_integration: MockConfigEntry, entity_id: str
Expand Down

0 comments on commit a1b84f3

Please sign in to comment.