Skip to content

Commit

Permalink
Update PyPlumIO to 0.5.8.
Browse files Browse the repository at this point in the history
Update RegdataSensor to handle new regdata implementation in PyPlumIO.
  • Loading branch information
denpamusic committed Nov 26, 2023
1 parent 9097be7 commit 6a27590
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pyplumio==0.5.7 pytest-homeassistant-custom-component psutil-home-assistant fnv-hash-fast aiohttp_cors mypy pylint flake8 flake8-pyproject black
python -m pip install pyplumio==0.5.8 pytest-homeassistant-custom-component psutil-home-assistant fnv-hash-fast aiohttp_cors mypy pylint flake8 flake8-pyproject black
- name: Check typing
run: |
Expand Down
2 changes: 1 addition & 1 deletion custom_components/plum_ecomax/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ATTR_PASSWORD: Final = "password"
ATTR_PRODUCT: Final = "product"
ATTR_PRESET: Final = "preset"
ATTR_REGDATA: Final = "regdata"
ATTR_SCHEDULES: Final = "schedules"
ATTR_SENSORS: Final = "sensors"
ATTR_START: Final = "start"
Expand All @@ -35,7 +36,6 @@
ATTR_WATER_HEATER_TEMP: Final = "water_heater_temp"
ATTR_WEEKDAYS: Final = "weekdays"
ATTR_FIRMWARE: Final = "firmware"
ATTR_REGDATA: Final = "regdata"

# Devices.
ECOMAX: Final = "ecomax"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/plum_ecomax/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EcomaxEntity(ABC):
async def async_added_to_hass(self):
"""Called when an entity has their entity_id assigned."""

async def async_set_available(value=None):
async def async_set_available(_=None) -> None:
self._attr_available = True

func = self.entity_description.filter_fn(self.async_update)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/plum_ecomax/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"pyplumio"
],
"requirements": [
"pyplumio==0.5.7"
"pyplumio==0.5.8"
],
"version": "0.4.1-beta.2"
}
36 changes: 32 additions & 4 deletions custom_components/plum_ecomax/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
from pyplumio.const import DeviceState, ProductType
from pyplumio.filters import aggregate, on_change, throttle
from pyplumio.structures.modules import ConnectedModules
from pyplumio.structures.regulator_data import RegulatorData
import voluptuous as vol

from .connection import EcomaxConnection
from .const import (
ALL,
ATTR_NUMERIC,
ATTR_REGDATA,
ATTR_VALUE,
DEVICE_CLASS_METER,
DEVICE_CLASS_STATE,
Expand Down Expand Up @@ -515,10 +515,38 @@ class RegdataSensorEntityDescription(EcomaxSensorEntityDescription):
class RegdataSensor(EcomaxSensor):
"""Represents RegData sensor platform."""

async def async_update(self, value: dict[str, Any]) -> None:
"""Update entity state."""
self._attr_native_value = (
self.entity_description.value_fn(value[self.entity_description.key])
if self.entity_description.key in value
else None
)
self.async_write_ha_state()

async def async_added_to_hass(self):
"""Called when an entity has their entity_id assigned."""

async def async_set_available(regdata: dict[str, Any]) -> None:
if self.entity_description.key in regdata:
self._attr_available = True

func = self.entity_description.filter_fn(self.async_update)
self.device.subscribe_once(ATTR_REGDATA, async_set_available)
self.device.subscribe(ATTR_REGDATA, func)

if ATTR_REGDATA in self.device.data:
await async_set_available(self.device.data[ATTR_REGDATA])
await func(self.device.data[ATTR_REGDATA])

async def async_will_remove_from_hass(self):
"""Called when an entity is about to be removed."""
self.device.unsubscribe(ATTR_REGDATA, self.async_update)

@property
def device(self) -> RegulatorData:
"""Return device object."""
return self.connection.device.regdata
def entity_registry_enabled_default(self) -> bool:
"""Indicate if the entity should be enabled when first added."""
return self.entity_description.key in self.device.data.get(ATTR_REGDATA, {})


def get_by_product_model(
Expand Down
5 changes: 2 additions & 3 deletions custom_components/plum_ecomax/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
)
from pyplumio.const import UnitOfMeasurement
from pyplumio.devices import Device
from pyplumio.exceptions import ParameterNotFoundError
from pyplumio.helpers.parameter import Parameter
from pyplumio.helpers.schedule import (
START_OF_DAY,
Expand Down Expand Up @@ -146,7 +145,7 @@ async def async_get_device_parameter(
"""Get device parameter."""
try:
parameter: Parameter = await device.get(name)
except (ParameterNotFoundError, TimeoutError):
except (TypeError, TimeoutError):
_LOGGER.exception("Requested parameter %s not found", name)
return None

Expand Down Expand Up @@ -208,7 +207,7 @@ async def async_set_device_parameter(device: Device, name: str, value: float) ->
"""Set device parameter."""
try:
return await device.set(name, value)
except (ParameterNotFoundError, TimeoutError):
except (TypeError, TimeoutError):
_LOGGER.exception("Requested parameter %s not found", name)
except ValueError as e:
raise HomeAssistantError(f"Couldn't set parameter: {e}") from e
Expand Down
19 changes: 7 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest.mock import AsyncMock, Mock, patch

from homeassistant.core import HomeAssistant
from pyplumio import Connection
from pyplumio.connection import Connection
from pyplumio.const import DeviceState, ProductType, UnitOfMeasurement
from pyplumio.devices.ecomax import EcoMAX
from pyplumio.devices.mixer import Mixer
Expand All @@ -25,7 +25,6 @@
from pyplumio.structures.modules import ConnectedModules
from pyplumio.structures.network_info import NetworkInfo
from pyplumio.structures.product_info import ProductInfo
from pyplumio.structures.regulator_data import RegulatorData
from pyplumio.structures.thermostat_parameters import (
ThermostatParameter,
ThermostatParameterDescription,
Expand All @@ -37,6 +36,8 @@
from custom_components.plum_ecomax.const import (
ATTR_ECOMAX_CONTROL,
ATTR_MIXERS,
ATTR_PRODUCT,
ATTR_REGDATA,
CONF_BAUDRATE,
CONF_CONNECTION_TYPE,
CONF_DEVICE,
Expand Down Expand Up @@ -401,20 +402,17 @@ def ecomax_860p3_o(ecomax_p: EcoMAX):
product_type = ProductType.ECOMAX_P
product_model = ProductModel.ECOMAX_860P3_O

regulator_data = RegulatorData()
regulator_data.data = load_regdata_fixture("regdata__ecomax_860p3_o.json")

ecomax_p.data.update(
{
"product": ProductInfo(
ATTR_PRODUCT: ProductInfo(
type=product_type,
id=51,
uid="TEST",
logo=13056,
image=2816,
model=product_model,
),
"regdata": regulator_data,
ATTR_REGDATA: load_regdata_fixture("regdata__ecomax_860p3_o.json"),
}
)

Expand All @@ -433,20 +431,17 @@ def ecomax_860p3_s_lite(ecomax_p: EcoMAX):
product_type = ProductType.ECOMAX_P
product_model = ProductModel.ECOMAX_860P3_S_LITE

regulator_data = RegulatorData()
regulator_data.data = load_regdata_fixture("regdata__ecomax_860p3_s_lite.json")

ecomax_p.data.update(
{
"product": ProductInfo(
ATTR_PRODUCT: ProductInfo(
type=product_type,
id=51,
uid="TEST",
logo=13056,
image=2816,
model=product_model,
),
"regdata": regulator_data,
ATTR_REGDATA: load_regdata_fixture("regdata__ecomax_860p3_s_lite.json"),
}
)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import DeviceInfo
from pyplumio import Connection, SerialConnection, TcpConnection
from pyplumio import SerialConnection, TcpConnection
from pyplumio.connection import Connection
from pyplumio.const import FrameType
from pyplumio.devices.ecomax import EcoMAX
import pytest
Expand Down
2 changes: 0 additions & 2 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
ATTR_MIXERS,
ATTR_PASSWORD,
ATTR_PRODUCT,
ATTR_REGDATA,
CONF_CONNECTION_TYPE,
CONF_HOST,
CONF_MODEL,
Expand Down Expand Up @@ -63,7 +62,6 @@ async def test_diagnostics(
ecomax_data = dict(ecomax_p.data)
ecomax_data[ATTR_PASSWORD] = REDACTED
ecomax_data[ATTR_MIXERS] = {x: y.data for x, y in ecomax_data[ATTR_MIXERS].items()}
ecomax_data[ATTR_REGDATA] = ecomax_data[ATTR_REGDATA].data
assert result["data"][ATTR_PRODUCT].uid == REDACTED
assert ecomax_data[ATTR_PRODUCT].uid != REDACTED

Expand Down
5 changes: 3 additions & 2 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
from custom_components.plum_ecomax.connection import EcomaxConnection
from custom_components.plum_ecomax.const import (
ATTR_NUMERIC,
ATTR_REGDATA,
ATTR_VALUE,
DEVICE_CLASS_METER,
DEVICE_CLASS_STATE,
Expand Down Expand Up @@ -1215,7 +1216,7 @@ async def test_ash_pan_full_sensor_ecomax_860p3_o(
assert state.attributes[ATTR_ICON] == "mdi:tray-alert"

# Dispatch new value.
await connection.device.regdata.dispatch(ash_pan_full_key, 55)
await connection.device.dispatch(ATTR_REGDATA, {ash_pan_full_key: 55})
state = hass.states.get(ash_pan_full_entity_id)
assert state.state == "55"

Expand Down Expand Up @@ -1247,6 +1248,6 @@ async def test_ash_pan_full_sensor_ecomax_860p3_s_lite(
assert state.attributes[ATTR_ICON] == "mdi:tray-alert"

# Dispatch new value.
await connection.device.regdata.dispatch(ash_pan_full_key, 55)
await connection.device.dispatch(ATTR_REGDATA, {ash_pan_full_key: 55})
state = hass.states.get(ash_pan_full_entity_id)
assert state.state == "55"
5 changes: 2 additions & 3 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceEntry
from pyplumio.devices.ecomax import EcoMAX
from pyplumio.exceptions import ParameterNotFoundError
from pyplumio.helpers.schedule import STATE_DAY, STATE_NIGHT, Schedule, ScheduleDay
import pytest
from pytest_homeassistant_custom_component.common import MockConfigEntry
Expand Down Expand Up @@ -151,7 +150,7 @@ async def test_get_parameter_service(

# Test getting nonexistent parameter.
with pytest.raises(HomeAssistantError), patch(
"pyplumio.devices.Device.get", side_effect=ParameterNotFoundError
"pyplumio.devices.Device.get", side_effect=TypeError
):
await hass.services.async_call(
DOMAIN,
Expand Down Expand Up @@ -260,7 +259,7 @@ async def test_set_parameter_service(

# Test setting parameter to nonexistent parameter.
with pytest.raises(HomeAssistantError), patch(
"pyplumio.devices.Device.set", side_effect=ParameterNotFoundError
"pyplumio.devices.Device.set", side_effect=TypeError
) as mock_set:
await hass.services.async_call(
DOMAIN,
Expand Down

0 comments on commit 6a27590

Please sign in to comment.