Various improvement for JustNimbus (#76858)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof 2022-08-16 17:10:11 +02:00 committed by GitHub
parent 93a72982ce
commit d50b5cebee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 12 deletions

View File

@ -1,19 +1,15 @@
"""Base Entity for JustNimbus sensors.""" """Base Entity for JustNimbus sensors."""
from __future__ import annotations from __future__ import annotations
import justnimbus
from homeassistant.components.sensor import SensorEntity
from homeassistant.helpers import update_coordinator
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import JustNimbusCoordinator
from .const import DOMAIN from .const import DOMAIN
from .coordinator import JustNimbusCoordinator
class JustNimbusEntity( class JustNimbusEntity(
update_coordinator.CoordinatorEntity[justnimbus.JustNimbusModel], CoordinatorEntity[JustNimbusCoordinator],
SensorEntity,
): ):
"""Defines a base JustNimbus entity.""" """Defines a base JustNimbus entity."""

View File

@ -7,6 +7,7 @@ from typing import Any
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
@ -15,6 +16,7 @@ from homeassistant.const import (
CONF_CLIENT_ID, CONF_CLIENT_ID,
PRESSURE_BAR, PRESSURE_BAR,
TEMP_CELSIUS, TEMP_CELSIUS,
TIME_HOURS,
VOLUME_LITERS, VOLUME_LITERS,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -82,6 +84,7 @@ SENSOR_TYPES = (
name="Pump hours", name="Pump hours",
icon="mdi:clock", icon="mdi:clock",
device_class=SensorDeviceClass.DURATION, device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=TIME_HOURS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda coordinator: coordinator.data.pump_hours, value_fn=lambda coordinator: coordinator.data.pump_hours,
@ -127,7 +130,6 @@ SENSOR_TYPES = (
name="Error code", name="Error code",
icon="mdi:bug", icon="mdi:bug",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
native_unit_of_measurement="",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda coordinator: coordinator.data.error_code, value_fn=lambda coordinator: coordinator.data.error_code,
), ),
@ -167,9 +169,7 @@ async def async_setup_entry(
) )
class JustNimbusSensor( class JustNimbusSensor(JustNimbusEntity, SensorEntity):
JustNimbusEntity,
):
"""Implementation of the JustNimbus sensor.""" """Implementation of the JustNimbus sensor."""
def __init__( def __init__(

View File

@ -10,6 +10,8 @@ from homeassistant.const import CONF_CLIENT_ID
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
async def test_form(hass: HomeAssistant) -> None: async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form.""" """Test we get the form."""
@ -30,9 +32,13 @@ async def test_form(hass: HomeAssistant) -> None:
{"base": "invalid_auth"}, {"base": "invalid_auth"},
), ),
( (
JustNimbusError(), JustNimbusError,
{"base": "cannot_connect"}, {"base": "cannot_connect"},
), ),
(
RuntimeError,
{"base": "unknown"},
),
), ),
) )
async def test_form_errors( async def test_form_errors(
@ -62,6 +68,34 @@ async def test_form_errors(
await _set_up_justnimbus(hass=hass, flow_id=result["flow_id"]) await _set_up_justnimbus(hass=hass, flow_id=result["flow_id"])
async def test_abort_already_configured(hass: HomeAssistant) -> None:
"""Test we abort when the device is already configured."""
entry = MockConfigEntry(
domain=DOMAIN,
title="JustNimbus",
data={CONF_CLIENT_ID: "test_id"},
unique_id="test_id",
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result.get("type") == FlowResultType.FORM
assert result.get("errors") is None
assert "flow_id" in result
result2 = await hass.config_entries.flow.async_configure(
flow_id=result["flow_id"],
user_input={
CONF_CLIENT_ID: "test_id",
},
)
assert result2.get("type") == FlowResultType.ABORT
assert result2.get("reason") == "already_configured"
async def _set_up_justnimbus(hass: HomeAssistant, flow_id: str) -> None: async def _set_up_justnimbus(hass: HomeAssistant, flow_id: str) -> None:
"""Reusable successful setup of JustNimbus sensor.""" """Reusable successful setup of JustNimbus sensor."""
with patch("justnimbus.JustNimbusClient.get_data"), patch( with patch("justnimbus.JustNimbusClient.get_data"), patch(