Add Flipr binary sensor (#53525)

pull/55900/head
cnico 2021-09-07 09:52:42 +02:00 committed by GitHub
parent 2f3a11f930
commit 53ea24ec15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 81 deletions

View File

@ -5,21 +5,22 @@ import logging
from flipr_api import FliprAPIRestClient from flipr_api import FliprAPIRestClient
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.const import ATTR_ATTRIBUTION, CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
) )
from .const import CONF_FLIPR_ID, DOMAIN, MANUFACTURER, NAME from .const import ATTRIBUTION, CONF_FLIPR_ID, DOMAIN, MANUFACTURER, NAME
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(minutes=60) SCAN_INTERVAL = timedelta(minutes=60)
PLATFORMS = ["sensor"] PLATFORMS = ["sensor", "binary_sensor"]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@ -75,14 +76,22 @@ class FliprDataUpdateCoordinator(DataUpdateCoordinator):
class FliprEntity(CoordinatorEntity): class FliprEntity(CoordinatorEntity):
"""Implements a common class elements representing the Flipr component.""" """Implements a common class elements representing the Flipr component."""
def __init__(self, coordinator, flipr_id, info_type): _attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
def __init__(
self, coordinator: DataUpdateCoordinator, description: EntityDescription
) -> None:
"""Initialize Flipr sensor.""" """Initialize Flipr sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._attr_unique_id = f"{flipr_id}-{info_type}" self.entity_description = description
if coordinator.config_entry:
flipr_id = coordinator.config_entry.data[CONF_FLIPR_ID]
self._attr_unique_id = f"{flipr_id}-{description.key}"
self._attr_device_info = { self._attr_device_info = {
"identifiers": {(DOMAIN, flipr_id)}, "identifiers": {(DOMAIN, flipr_id)},
"name": NAME, "name": NAME,
"manufacturer": MANUFACTURER, "manufacturer": MANUFACTURER,
} }
self.info_type = info_type
self.flipr_id = flipr_id self._attr_name = f"Flipr {flipr_id} {description.name}"

View File

@ -0,0 +1,46 @@
"""Support for Flipr binary sensors."""
from __future__ import annotations
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_PROBLEM,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from . import FliprEntity
from .const import DOMAIN
BINARY_SENSORS_TYPES: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="ph_status",
name="PH Status",
device_class=DEVICE_CLASS_PROBLEM,
),
BinarySensorEntityDescription(
key="chlorine_status",
name="Chlorine Status",
device_class=DEVICE_CLASS_PROBLEM,
),
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Defer sensor setup of flipr binary sensors."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities(
FliprBinarySensor(coordinator, description)
for description in BINARY_SENSORS_TYPES
)
class FliprBinarySensor(FliprEntity, BinarySensorEntity):
"""Representation of Flipr binary sensors."""
@property
def is_on(self):
"""Return true if the binary sensor is on in case of a Problem is detected."""
return (
self.coordinator.data[self.entity_description.key] == "TooLow"
or self.coordinator.data[self.entity_description.key] == "TooHigh"
)

View File

@ -1,9 +1,10 @@
"""Sensor platform for the Flipr's pool_sensor.""" """Sensor platform for the Flipr's pool_sensor."""
from __future__ import annotations
from datetime import datetime from datetime import datetime
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import ( from homeassistant.const import (
ATTR_ATTRIBUTION,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_TIMESTAMP,
ELECTRIC_POTENTIAL_MILLIVOLT, ELECTRIC_POTENTIAL_MILLIVOLT,
@ -11,78 +12,55 @@ from homeassistant.const import (
) )
from . import FliprEntity from . import FliprEntity
from .const import ATTRIBUTION, CONF_FLIPR_ID, DOMAIN from .const import DOMAIN
SENSORS = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
"chlorine": { SensorEntityDescription(
"unit": ELECTRIC_POTENTIAL_MILLIVOLT, key="chlorine",
"icon": "mdi:pool", name="Chlorine",
"name": "Chlorine", native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
"device_class": None, icon="mdi:pool",
}, ),
"ph": {"unit": None, "icon": "mdi:pool", "name": "pH", "device_class": None}, SensorEntityDescription(
"temperature": { key="ph",
"unit": TEMP_CELSIUS, name="pH",
"icon": None, icon="mdi:pool",
"name": "Water Temp", ),
"device_class": DEVICE_CLASS_TEMPERATURE, SensorEntityDescription(
}, key="temperature",
"date_time": { name="Water Temp",
"unit": None, device_class=DEVICE_CLASS_TEMPERATURE,
"icon": None, native_unit_of_measurement=TEMP_CELSIUS,
"name": "Last Measured", ),
"device_class": DEVICE_CLASS_TIMESTAMP, SensorEntityDescription(
}, key="date_time",
"red_ox": { name="Last Measured",
"unit": ELECTRIC_POTENTIAL_MILLIVOLT, device_class=DEVICE_CLASS_TIMESTAMP,
"icon": "mdi:pool", ),
"name": "Red OX", SensorEntityDescription(
"device_class": None, key="red_ox",
}, name="Red OX",
} native_unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
icon="mdi:pool",
),
)
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Defer sensor setup to the shared sensor module.""" """Defer sensor setup to the shared sensor module."""
flipr_id = config_entry.data[CONF_FLIPR_ID]
coordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = hass.data[DOMAIN][config_entry.entry_id]
sensors_list = [] sensors = [FliprSensor(coordinator, description) for description in SENSOR_TYPES]
for sensor in SENSORS: async_add_entities(sensors, True)
sensors_list.append(FliprSensor(coordinator, flipr_id, sensor))
async_add_entities(sensors_list, True)
class FliprSensor(FliprEntity, SensorEntity): class FliprSensor(FliprEntity, SensorEntity):
"""Sensor representing FliprSensor data.""" """Sensor representing FliprSensor data."""
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
@property
def name(self):
"""Return the name of the particular component."""
return f"Flipr {self.flipr_id} {SENSORS[self.info_type]['name']}"
@property @property
def native_value(self): def native_value(self):
"""State of the sensor.""" """State of the sensor."""
state = self.coordinator.data[self.info_type] state = self.coordinator.data[self.entity_description.key]
if isinstance(state, datetime): if isinstance(state, datetime):
return state.isoformat() return state.isoformat()
return state return state
@property
def device_class(self):
"""Return the device class."""
return SENSORS[self.info_type]["device_class"]
@property
def icon(self):
"""Return the icon."""
return SENSORS[self.info_type]["icon"]
@property
def native_unit_of_measurement(self):
"""Return unit of measurement."""
return SENSORS[self.info_type]["unit"]

View File

@ -3,7 +3,6 @@ from datetime import datetime
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.flipr.const import CONF_FLIPR_ID, DOMAIN from homeassistant.components.flipr.const import CONF_FLIPR_ID, DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import ( from homeassistant.const import (
ATTR_ICON, ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
@ -45,15 +44,6 @@ async def test_sensors(hass: HomeAssistant) -> None:
registry = await hass.helpers.entity_registry.async_get_registry() registry = await hass.helpers.entity_registry.async_get_registry()
# Pre-create registry entries for sensors
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"my_random_entity_id",
suggested_object_id="sensor.flipr_myfliprid_chlorine",
disabled_by=None,
)
with patch( with patch(
"flipr_api.FliprAPIRestClient.get_pool_measure_latest", "flipr_api.FliprAPIRestClient.get_pool_measure_latest",
return_value=MOCK_FLIPR_MEASURE, return_value=MOCK_FLIPR_MEASURE,
@ -61,6 +51,10 @@ async def test_sensors(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
# Check entity unique_id value that is generated in FliprEntity base class.
entity = registry.async_get("sensor.flipr_myfliprid_red_ox")
assert entity.unique_id == "myfliprid-red_ox"
state = hass.states.get("sensor.flipr_myfliprid_ph") state = hass.states.get("sensor.flipr_myfliprid_ph")
assert state assert state
assert state.attributes.get(ATTR_ICON) == "mdi:pool" assert state.attributes.get(ATTR_ICON) == "mdi:pool"
@ -90,3 +84,11 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert state.attributes.get(ATTR_ICON) == "mdi:pool" assert state.attributes.get(ATTR_ICON) == "mdi:pool"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "mV" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "mV"
assert state.state == "0.23654886" assert state.state == "0.23654886"
state = hass.states.get("binary_sensor.flipr_myfliprid_ph_status")
assert state
assert state.state == "on" # Alert is on for binary sensor
state = hass.states.get("binary_sensor.flipr_myfliprid_chlorine_status")
assert state
assert state.state == "off"