Refactor Netatmo integration (#29851)
* Refactor to use ids in data class * Use station_id * Refactor Netatmo to use oauth * Remove old code * Clean up * Clean up * Clean up * Refactor binary sensor * Add initial light implementation * Add discovery * Add set schedule service back in * Add discovery via homekit * More work on the light * Fix set schedule service * Clean up * Remove unnecessary code * Add support for multiple entities/accounts * Fix MANUFACTURER typo * Remove multiline inline if statement * Only add tags when camera type is welcome * Remove on/off as it's currently broken * Fix camera turn_on/off * Fix debug message * Refactor some camera code * Refactor camera methods * Remove old code * Rename method * Update persons regularly * Remove unused code * Refactor method * Fix isort * Add english strings * Catch NoDevice exception * Fix unique id and only add sensors for tags if present * Address comments * Remove ToDo comment * Add set_light_auto back in * Add debug info * Fix multiple camera issue * Move camera light service to camera * Only allow camera entities * Make test pass * Upgrade pyatmo module to 3.2.0 * Update requirements * Remove list comprehension * Remove guideline violating code * Remove stale code * Rename devices to entities * Remove light platform * Remove commented code * Exclude files from coverage * Remove unused code * Fix unique id * Address comments * Fix comments * Exclude sensor as well * Add another test * Use core interfaces
This commit is contained in:
1 parent
5ffbf55170
commit
e793ed9ab0
19 files changed
+762
-927
No files matched your search
@@ -1,29 +1,20 @@
|
||||
"""Support for the Netatmo Weather Service."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import threading
|
||||
from time import time
|
||||
|
||||
import pyatmo
|
||||
import requests
|
||||
import urllib3
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
CONF_MODE,
|
||||
CONF_NAME,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import call_later
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
from .const import DATA_NETATMO_AUTH, DOMAIN
|
||||
from .const import AUTH, DOMAIN, MANUFACTURER
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -38,13 +29,11 @@ CONF_LON_SW = "lon_sw"
|
||||
DEFAULT_MODE = "avg"
|
||||
MODE_TYPES = {"max", "avg"}
|
||||
|
||||
DEFAULT_NAME_PUBLIC = "Netatmo Public Data"
|
||||
|
||||
# This is the Netatmo data upload interval in seconds
|
||||
NETATMO_UPDATE_INTERVAL = 600
|
||||
|
||||
# NetAtmo Public Data is uploaded to server every 10 minutes
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=600)
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=NETATMO_UPDATE_INTERVAL)
|
||||
|
||||
SUPPORTED_PUBLIC_SENSOR_TYPES = [
|
||||
"temperature",
|
||||
@@ -90,26 +79,6 @@ SENSOR_TYPES = {
|
||||
"health_idx": ["Health", "", "mdi:cloud", None],
|
||||
}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_STATION): cv.string,
|
||||
vol.Optional(CONF_MODULES): vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_AREAS): vol.All(
|
||||
cv.ensure_list,
|
||||
[
|
||||
{
|
||||
vol.Required(CONF_LAT_NE): cv.latitude,
|
||||
vol.Required(CONF_LAT_SW): cv.latitude,
|
||||
vol.Required(CONF_LON_NE): cv.longitude,
|
||||
vol.Required(CONF_LON_SW): cv.longitude,
|
||||
vol.Optional(CONF_MODE, default=DEFAULT_MODE): vol.In(MODE_TYPES),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME_PUBLIC): cv.string,
|
||||
}
|
||||
],
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
MODULE_TYPE_OUTDOOR = "NAModule1"
|
||||
MODULE_TYPE_WIND = "NAModule2"
|
||||
MODULE_TYPE_RAIN = "NAModule3"
|
||||
@@ -122,75 +91,47 @@ NETATMO_DEVICE_TYPES = {
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the available Netatmo weather sensors."""
|
||||
dev = []
|
||||
auth = hass.data[DATA_NETATMO_AUTH]
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
"""Set up the Netatmo weather and homecoach platform."""
|
||||
auth = hass.data[DOMAIN][entry.entry_id][AUTH]
|
||||
|
||||
if config.get(CONF_AREAS) is not None:
|
||||
for area in config[CONF_AREAS]:
|
||||
data = NetatmoPublicData(
|
||||
auth,
|
||||
lat_ne=area[CONF_LAT_NE],
|
||||
lon_ne=area[CONF_LON_NE],
|
||||
lat_sw=area[CONF_LAT_SW],
|
||||
lon_sw=area[CONF_LON_SW],
|
||||
)
|
||||
for sensor_type in SUPPORTED_PUBLIC_SENSOR_TYPES:
|
||||
dev.append(
|
||||
NetatmoPublicSensor(
|
||||
area[CONF_NAME], data, sensor_type, area[CONF_MODE]
|
||||
)
|
||||
)
|
||||
else:
|
||||
def find_entities(data):
|
||||
"""Find all entities."""
|
||||
all_module_infos = data.get_module_infos()
|
||||
entities = []
|
||||
for module in all_module_infos.values():
|
||||
_LOGGER.debug("Adding module %s %s", module["module_name"], module["id"])
|
||||
for condition in data.station_data.monitoredConditions(
|
||||
moduleId=module["id"]
|
||||
):
|
||||
entities.append(NetatmoSensor(data, module, condition.lower()))
|
||||
return entities
|
||||
|
||||
def find_devices(data):
|
||||
"""Find all devices."""
|
||||
all_module_infos = data.get_module_infos()
|
||||
all_module_names = [e["module_name"] for e in all_module_infos.values()]
|
||||
module_names = config.get(CONF_MODULES, all_module_names)
|
||||
entities = []
|
||||
for module_name in module_names:
|
||||
if module_name not in all_module_names:
|
||||
_LOGGER.info("Module %s not found", module_name)
|
||||
for module in all_module_infos.values():
|
||||
if module["module_name"] not in module_names:
|
||||
continue
|
||||
_LOGGER.debug(
|
||||
"Adding module %s %s", module["module_name"], module["id"]
|
||||
)
|
||||
for condition in data.station_data.monitoredConditions(
|
||||
moduleId=module["id"]
|
||||
):
|
||||
entities.append(NetatmoSensor(data, module, condition.lower()))
|
||||
return entities
|
||||
|
||||
def _retry(_data):
|
||||
try:
|
||||
entities = find_devices(_data)
|
||||
except requests.exceptions.Timeout:
|
||||
return call_later(
|
||||
hass, NETATMO_UPDATE_INTERVAL, lambda _: _retry(_data)
|
||||
)
|
||||
if entities:
|
||||
add_entities(entities, True)
|
||||
def get_entities():
|
||||
"""Retrieve Netatmo entities."""
|
||||
entities = []
|
||||
|
||||
for data_class in [pyatmo.WeatherStationData, pyatmo.HomeCoachData]:
|
||||
try:
|
||||
data = NetatmoData(auth, data_class, config.get(CONF_STATION))
|
||||
dc_data = data_class(auth)
|
||||
_LOGGER.debug("%s detected!", NETATMO_DEVICE_TYPES[data_class.__name__])
|
||||
data = NetatmoData(auth, dc_data)
|
||||
except pyatmo.NoDevice:
|
||||
_LOGGER.info(
|
||||
"No %s devices found", NETATMO_DEVICE_TYPES[data_class.__name__]
|
||||
_LOGGER.debug(
|
||||
"No %s entities found", NETATMO_DEVICE_TYPES[data_class.__name__]
|
||||
)
|
||||
continue
|
||||
|
||||
try:
|
||||
dev.extend(find_devices(data))
|
||||
except requests.exceptions.Timeout:
|
||||
call_later(hass, NETATMO_UPDATE_INTERVAL, lambda _: _retry(data))
|
||||
entities.extend(find_entities(data))
|
||||
|
||||
if dev:
|
||||
add_entities(dev, True)
|
||||
return entities
|
||||
|
||||
async_add_entities(await hass.async_add_executor_job(get_entities), True)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the Netatmo weather and homecoach platform."""
|
||||
return
|
||||
|
||||
|
||||
class NetatmoSensor(Entity):
|
||||
@@ -212,7 +153,7 @@ class NetatmoSensor(Entity):
|
||||
f"{module_info['station_name']} {module_info['module_name']}"
|
||||
)
|
||||
|
||||
self._name = f"{DOMAIN} {self.module_name} {SENSOR_TYPES[sensor_type][0]}"
|
||||
self._name = f"{MANUFACTURER} {self.module_name} {SENSOR_TYPES[sensor_type][0]}"
|
||||
self.type = sensor_type
|
||||
self._state = None
|
||||
self._device_class = SENSOR_TYPES[self.type][3]
|
||||
@@ -237,6 +178,16 @@ class NetatmoSensor(Entity):
|
||||
"""Return the device class of the sensor."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return the device info for the sensor."""
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self._module_id)},
|
||||
"name": self.module_name,
|
||||
"manufacturer": MANUFACTURER,
|
||||
"model": self._module_type,
|
||||
}
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
@@ -258,14 +209,15 @@ class NetatmoSensor(Entity):
|
||||
if self.netatmo_data.data is None:
|
||||
if self._state is None:
|
||||
return
|
||||
_LOGGER.warning("No data found for %s", self.module_name)
|
||||
_LOGGER.warning("No data from update")
|
||||
self._state = None
|
||||
return
|
||||
|
||||
data = self.netatmo_data.data.get(self._module_id)
|
||||
|
||||
if data is None:
|
||||
_LOGGER.warning("No data found for %s", self.module_name)
|
||||
_LOGGER.info("No data found for %s (%s)", self.module_name, self._module_id)
|
||||
_LOGGER.error("data: %s", self.netatmo_data.data)
|
||||
self._state = None
|
||||
return
|
||||
|
||||
@@ -420,7 +372,7 @@ class NetatmoSensor(Entity):
|
||||
elif data["health_idx"] == 4:
|
||||
self._state = "Unhealthy"
|
||||
except KeyError:
|
||||
_LOGGER.error("No %s data found for %s", self.type, self.module_name)
|
||||
_LOGGER.info("No %s data found for %s", self.type, self.module_name)
|
||||
self._state = None
|
||||
return
|
||||
|
||||
@@ -433,7 +385,7 @@ class NetatmoPublicSensor(Entity):
|
||||
self.netatmo_data = data
|
||||
self.type = sensor_type
|
||||
self._mode = mode
|
||||
self._name = "{} {}".format(area_name, SENSOR_TYPES[self.type][0])
|
||||
self._name = f"{MANUFACTURER} {area_name} {SENSOR_TYPES[self.type][0]}"
|
||||
self._area_name = area_name
|
||||
self._state = None
|
||||
self._device_class = SENSOR_TYPES[self.type][3]
|
||||
@@ -455,6 +407,16 @@ class NetatmoPublicSensor(Entity):
|
||||
"""Return the device class of the sensor."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return the device info for the sensor."""
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self._area_name)},
|
||||
"name": self._area_name,
|
||||
"manufacturer": MANUFACTURER,
|
||||
"model": "public",
|
||||
}
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
@@ -470,7 +432,7 @@ class NetatmoPublicSensor(Entity):
|
||||
self.netatmo_data.update()
|
||||
|
||||
if self.netatmo_data.data is None:
|
||||
_LOGGER.warning("No data found for %s", self._name)
|
||||
_LOGGER.info("No data found for %s", self._name)
|
||||
self._state = None
|
||||
return
|
||||
|
||||
@@ -522,14 +484,21 @@ class NetatmoPublicData:
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Request an update from the Netatmo API."""
|
||||
data = pyatmo.PublicData(
|
||||
self.auth,
|
||||
LAT_NE=self.lat_ne,
|
||||
LON_NE=self.lon_ne,
|
||||
LAT_SW=self.lat_sw,
|
||||
LON_SW=self.lon_sw,
|
||||
filtering=True,
|
||||
)
|
||||
try:
|
||||
data = pyatmo.PublicData(
|
||||
self.auth,
|
||||
LAT_NE=self.lat_ne,
|
||||
LON_NE=self.lon_ne,
|
||||
LAT_SW=self.lat_sw,
|
||||
LON_SW=self.lon_sw,
|
||||
filtering=True,
|
||||
)
|
||||
except pyatmo.NoDevice:
|
||||
data = None
|
||||
|
||||
if not data:
|
||||
_LOGGER.debug("No data received when updating public station data")
|
||||
return
|
||||
|
||||
if data.CountStationInArea() == 0:
|
||||
_LOGGER.warning("No Stations available in this area.")
|
||||
@@ -541,83 +510,24 @@ class NetatmoPublicData:
|
||||
class NetatmoData:
|
||||
"""Get the latest data from Netatmo."""
|
||||
|
||||
def __init__(self, auth, data_class, station):
|
||||
def __init__(self, auth, station_data):
|
||||
"""Initialize the data object."""
|
||||
self.auth = auth
|
||||
self.data_class = data_class
|
||||
self.data = {}
|
||||
self.station_data = self.data_class(self.auth)
|
||||
self.station = station
|
||||
self.station_id = None
|
||||
if station:
|
||||
station_data = self.station_data.stationByName(self.station)
|
||||
if station_data:
|
||||
self.station_id = station_data.get("_id")
|
||||
self.station_data = station_data
|
||||
self._next_update = time()
|
||||
self._update_in_progress = threading.Lock()
|
||||
self.auth = auth
|
||||
|
||||
def get_module_infos(self):
|
||||
"""Return all modules available on the API as a dict."""
|
||||
if self.station_id is not None:
|
||||
return self.station_data.getModules(station_id=self.station_id)
|
||||
return self.station_data.getModules()
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Call the Netatmo API to update the data.
|
||||
"""Call the Netatmo API to update the data."""
|
||||
self.station_data = self.station_data.__class__(self.auth)
|
||||
|
||||
This method is not throttled by the builtin Throttle decorator
|
||||
but with a custom logic, which takes into account the time
|
||||
of the last update from the cloud.
|
||||
"""
|
||||
if time() < self._next_update or not self._update_in_progress.acquire(False):
|
||||
data = self.station_data.lastData(exclude=3600, byId=True)
|
||||
if not data:
|
||||
_LOGGER.debug("No data received when updating station data")
|
||||
return
|
||||
try:
|
||||
try:
|
||||
self.station_data = self.data_class(self.auth)
|
||||
_LOGGER.debug("%s detected!", str(self.data_class.__name__))
|
||||
except pyatmo.NoDevice:
|
||||
_LOGGER.warning(
|
||||
"No Weather or HomeCoach devices found for %s", str(self.station)
|
||||
)
|
||||
return
|
||||
except (requests.exceptions.Timeout, urllib3.exceptions.ReadTimeoutError):
|
||||
_LOGGER.warning("Timed out when connecting to Netatmo server.")
|
||||
return
|
||||
|
||||
data = self.station_data.lastData(
|
||||
station=self.station_id, exclude=3600, byId=True
|
||||
)
|
||||
if not data:
|
||||
self._next_update = time() + NETATMO_UPDATE_INTERVAL
|
||||
return
|
||||
self.data = data
|
||||
|
||||
newinterval = 0
|
||||
try:
|
||||
for module in self.data:
|
||||
if "When" in self.data[module]:
|
||||
newinterval = self.data[module]["When"]
|
||||
break
|
||||
except TypeError:
|
||||
_LOGGER.debug("No %s modules found", self.data_class.__name__)
|
||||
|
||||
if newinterval:
|
||||
# Try and estimate when fresh data will be available
|
||||
newinterval += NETATMO_UPDATE_INTERVAL - time()
|
||||
if newinterval > NETATMO_UPDATE_INTERVAL - 30:
|
||||
newinterval = NETATMO_UPDATE_INTERVAL
|
||||
else:
|
||||
if newinterval < NETATMO_UPDATE_INTERVAL / 2:
|
||||
# Never hammer the Netatmo API more than
|
||||
# twice per update interval
|
||||
newinterval = NETATMO_UPDATE_INTERVAL / 2
|
||||
_LOGGER.info(
|
||||
"Netatmo refresh interval reset to %d seconds", newinterval
|
||||
)
|
||||
else:
|
||||
# Last update time not found, fall back to default value
|
||||
newinterval = NETATMO_UPDATE_INTERVAL
|
||||
|
||||
self._next_update = time() + newinterval
|
||||
finally:
|
||||
self._update_in_progress.release()
|
||||
self.data = data
|
||||
Reference in new issue
Block a user