Add DataUpdateCoordinator to Verisure (#47574)

This commit is contained in:
Franck Nijhof 2021-03-11 19:41:01 +01:00 committed by GitHub
parent 10848b9bdf
commit 1095905f8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 287 additions and 475 deletions

View file

@ -6,9 +6,10 @@ from typing import Any, Callable
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import HUB as hub
from .const import CONF_HYDROMETERS, CONF_MOUSE, CONF_THERMOMETERS
from . import VerisureDataUpdateCoordinator
from .const import CONF_HYDROMETERS, CONF_MOUSE, CONF_THERMOMETERS, DOMAIN
def setup_platform(
@ -18,34 +19,34 @@ def setup_platform(
discovery_info: dict[str, Any] | None = None,
) -> None:
"""Set up the Verisure platform."""
sensors = []
hub.update_overview()
coordinator = hass.data[DOMAIN]
if int(hub.config.get(CONF_THERMOMETERS, 1)):
sensors = []
if int(coordinator.config.get(CONF_THERMOMETERS, 1)):
sensors.extend(
[
VerisureThermometer(device_label)
for device_label in hub.get(
VerisureThermometer(coordinator, device_label)
for device_label in coordinator.get(
"$.climateValues[?(@.temperature)].deviceLabel"
)
]
)
if int(hub.config.get(CONF_HYDROMETERS, 1)):
if int(coordinator.config.get(CONF_HYDROMETERS, 1)):
sensors.extend(
[
VerisureHygrometer(device_label)
for device_label in hub.get(
VerisureHygrometer(coordinator, device_label)
for device_label in coordinator.get(
"$.climateValues[?(@.humidity)].deviceLabel"
)
]
)
if int(hub.config.get(CONF_MOUSE, 1)):
if int(coordinator.config.get(CONF_MOUSE, 1)):
sensors.extend(
[
VerisureMouseDetection(device_label)
for device_label in hub.get(
VerisureMouseDetection(coordinator, device_label)
for device_label in coordinator.get(
"$.eventCounts[?(@.deviceType=='MOUSE1')].deviceLabel"
)
]
@ -54,18 +55,23 @@ def setup_platform(
add_entities(sensors)
class VerisureThermometer(Entity):
class VerisureThermometer(CoordinatorEntity, Entity):
"""Representation of a Verisure thermometer."""
def __init__(self, device_label: str):
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, device_label: str
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._device_label = device_label
@property
def name(self) -> str:
"""Return the name of the device."""
return (
hub.get_first(
self.coordinator.get_first(
"$.climateValues[?(@.deviceLabel=='%s')].deviceArea", self._device_label
)
+ " temperature"
@ -74,7 +80,7 @@ class VerisureThermometer(Entity):
@property
def state(self) -> str | None:
"""Return the state of the device."""
return hub.get_first(
return self.coordinator.get_first(
"$.climateValues[?(@.deviceLabel=='%s')].temperature", self._device_label
)
@ -82,7 +88,7 @@ class VerisureThermometer(Entity):
def available(self) -> bool:
"""Return True if entity is available."""
return (
hub.get_first(
self.coordinator.get_first(
"$.climateValues[?(@.deviceLabel=='%s')].temperature",
self._device_label,
)
@ -94,24 +100,24 @@ class VerisureThermometer(Entity):
"""Return the unit of measurement of this entity."""
return TEMP_CELSIUS
# pylint: disable=no-self-use
def update(self) -> None:
"""Update the sensor."""
hub.update_overview()
class VerisureHygrometer(Entity):
class VerisureHygrometer(CoordinatorEntity, Entity):
"""Representation of a Verisure hygrometer."""
def __init__(self, device_label: str):
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, device_label: str
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._device_label = device_label
@property
def name(self) -> str:
"""Return the name of the device."""
return (
hub.get_first(
self.coordinator.get_first(
"$.climateValues[?(@.deviceLabel=='%s')].deviceArea", self._device_label
)
+ " humidity"
@ -120,7 +126,7 @@ class VerisureHygrometer(Entity):
@property
def state(self) -> str | None:
"""Return the state of the device."""
return hub.get_first(
return self.coordinator.get_first(
"$.climateValues[?(@.deviceLabel=='%s')].humidity", self._device_label
)
@ -128,7 +134,7 @@ class VerisureHygrometer(Entity):
def available(self) -> bool:
"""Return True if entity is available."""
return (
hub.get_first(
self.coordinator.get_first(
"$.climateValues[?(@.deviceLabel=='%s')].humidity", self._device_label
)
is not None
@ -139,24 +145,24 @@ class VerisureHygrometer(Entity):
"""Return the unit of measurement of this entity."""
return PERCENTAGE
# pylint: disable=no-self-use
def update(self) -> None:
"""Update the sensor."""
hub.update_overview()
class VerisureMouseDetection(Entity):
class VerisureMouseDetection(CoordinatorEntity, Entity):
"""Representation of a Verisure mouse detector."""
def __init__(self, device_label):
coordinator: VerisureDataUpdateCoordinator
def __init__(
self, coordinator: VerisureDataUpdateCoordinator, device_label: str
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._device_label = device_label
@property
def name(self) -> str:
"""Return the name of the device."""
return (
hub.get_first(
self.coordinator.get_first(
"$.eventCounts[?(@.deviceLabel=='%s')].area", self._device_label
)
+ " mouse"
@ -165,7 +171,7 @@ class VerisureMouseDetection(Entity):
@property
def state(self) -> str | None:
"""Return the state of the device."""
return hub.get_first(
return self.coordinator.get_first(
"$.eventCounts[?(@.deviceLabel=='%s')].detections", self._device_label
)
@ -173,7 +179,9 @@ class VerisureMouseDetection(Entity):
def available(self) -> bool:
"""Return True if entity is available."""
return (
hub.get_first("$.eventCounts[?(@.deviceLabel=='%s')]", self._device_label)
self.coordinator.get_first(
"$.eventCounts[?(@.deviceLabel=='%s')]", self._device_label
)
is not None
)
@ -181,8 +189,3 @@ class VerisureMouseDetection(Entity):
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity."""
return "Mice"
# pylint: disable=no-self-use
def update(self) -> None:
"""Update the sensor."""
hub.update_overview()