mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Fix issue with multiple Netatmo home coach devices (#28407)
* Retrieve more detailed module infos * Switch to using IDs * Bump pyatmo version to 2.3.3 * Update requirements * Undo the change of the unique id * Rename variable
This commit is contained in:
parent
cffadf919a
commit
4435b3a5c9
@ -3,7 +3,7 @@
|
|||||||
"name": "Netatmo",
|
"name": "Netatmo",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/netatmo",
|
"documentation": "https://www.home-assistant.io/integrations/netatmo",
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"pyatmo==2.3.2"
|
"pyatmo==2.3.3"
|
||||||
],
|
],
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"webhook"
|
"webhook"
|
||||||
|
@ -6,6 +6,7 @@ from time import time
|
|||||||
|
|
||||||
import pyatmo
|
import pyatmo
|
||||||
import requests
|
import requests
|
||||||
|
import urllib3
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -21,7 +22,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import call_later
|
from homeassistant.helpers.event import call_later
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
from .const import DATA_NETATMO_AUTH
|
from .const import DATA_NETATMO_AUTH, DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -144,37 +145,36 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
|
|
||||||
def find_devices(data):
|
def find_devices(data):
|
||||||
"""Find all devices."""
|
"""Find all devices."""
|
||||||
all_module_names = data.get_module_names()
|
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)
|
module_names = config.get(CONF_MODULES, all_module_names)
|
||||||
_dev = []
|
entities = []
|
||||||
for module_name in module_names:
|
for module_name in module_names:
|
||||||
if module_name not in all_module_names:
|
if module_name not in all_module_names:
|
||||||
_LOGGER.info("Module %s not found", module_name)
|
_LOGGER.info("Module %s not found", module_name)
|
||||||
|
for module in all_module_infos.values():
|
||||||
|
if module["module_name"] not in module_names:
|
||||||
continue
|
continue
|
||||||
for condition in data.station_data.monitoredConditions(module_name):
|
for condition in data.station_data.monitoredConditions(
|
||||||
|
moduleId=module["id"]
|
||||||
|
):
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Adding %s %s",
|
"Adding %s %s",
|
||||||
module_name,
|
module["module_name"],
|
||||||
data.station_data.moduleByName(
|
data.station_data.moduleById(mid=module["id"]),
|
||||||
station=data.station, module=module_name
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
_dev.append(
|
entities.append(NetatmoSensor(data, module, condition.lower()))
|
||||||
NetatmoSensor(
|
return entities
|
||||||
data, module_name, condition.lower(), data.station
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return _dev
|
|
||||||
|
|
||||||
def _retry(_data):
|
def _retry(_data):
|
||||||
try:
|
try:
|
||||||
_dev = find_devices(_data)
|
entities = find_devices(_data)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return call_later(
|
return call_later(
|
||||||
hass, NETATMO_UPDATE_INTERVAL, lambda _: _retry(_data)
|
hass, NETATMO_UPDATE_INTERVAL, lambda _: _retry(_data)
|
||||||
)
|
)
|
||||||
if _dev:
|
if entities:
|
||||||
add_entities(_dev, True)
|
add_entities(entities, True)
|
||||||
|
|
||||||
for data_class in [pyatmo.WeatherStationData, pyatmo.HomeCoachData]:
|
for data_class in [pyatmo.WeatherStationData, pyatmo.HomeCoachData]:
|
||||||
try:
|
try:
|
||||||
@ -197,22 +197,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class NetatmoSensor(Entity):
|
class NetatmoSensor(Entity):
|
||||||
"""Implementation of a Netatmo sensor."""
|
"""Implementation of a Netatmo sensor."""
|
||||||
|
|
||||||
def __init__(self, netatmo_data, module_name, sensor_type, station):
|
def __init__(self, netatmo_data, module_info, sensor_type):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = "Netatmo {} {}".format(module_name, SENSOR_TYPES[sensor_type][0])
|
|
||||||
self.netatmo_data = netatmo_data
|
self.netatmo_data = netatmo_data
|
||||||
self.module_name = module_name
|
module = self.netatmo_data.station_data.moduleById(mid=module_info["id"])
|
||||||
|
if module["type"] == "NHC":
|
||||||
|
self.module_name = module_info["station_name"]
|
||||||
|
else:
|
||||||
|
self.module_name = module_info["module_name"]
|
||||||
|
self._name = f"{DOMAIN} {self.module_name} {SENSOR_TYPES[sensor_type][0]}"
|
||||||
self.type = sensor_type
|
self.type = sensor_type
|
||||||
self.station_name = station
|
|
||||||
self._state = None
|
self._state = None
|
||||||
self._device_class = SENSOR_TYPES[self.type][3]
|
self._device_class = SENSOR_TYPES[self.type][3]
|
||||||
self._icon = SENSOR_TYPES[self.type][2]
|
self._icon = SENSOR_TYPES[self.type][2]
|
||||||
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
||||||
module = self.netatmo_data.station_data.moduleByName(
|
|
||||||
station=self.station_name, module=module_name
|
|
||||||
)
|
|
||||||
self._module_type = module["type"]
|
self._module_type = module["type"]
|
||||||
self._unique_id = "{}-{}".format(module["_id"], self.type)
|
self._module_id = module_info["id"]
|
||||||
|
self._unique_id = f"{self._module_id}-{self.type}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -254,7 +255,7 @@ class NetatmoSensor(Entity):
|
|||||||
self._state = None
|
self._state = None
|
||||||
return
|
return
|
||||||
|
|
||||||
data = self.netatmo_data.data.get(self.module_name)
|
data = self.netatmo_data.data.get(self._module_id)
|
||||||
|
|
||||||
if data is None:
|
if data is None:
|
||||||
_LOGGER.warning("No data found for %s", self.module_name)
|
_LOGGER.warning("No data found for %s", self.module_name)
|
||||||
@ -543,11 +544,11 @@ class NetatmoData:
|
|||||||
self._next_update = time()
|
self._next_update = time()
|
||||||
self._update_in_progress = threading.Lock()
|
self._update_in_progress = threading.Lock()
|
||||||
|
|
||||||
def get_module_names(self):
|
def get_module_infos(self):
|
||||||
"""Return all module available on the API as a list."""
|
"""Return all modules available on the API as a dict."""
|
||||||
if self.station is not None:
|
if self.station is not None:
|
||||||
return self.station_data.modulesNamesList(station=self.station)
|
return self.station_data.getModules(station=self.station)
|
||||||
return self.station_data.modulesNamesList()
|
return self.station_data.getModules()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Call the Netatmo API to update the data.
|
"""Call the Netatmo API to update the data.
|
||||||
@ -567,14 +568,13 @@ class NetatmoData:
|
|||||||
"No Weather or HomeCoach devices found for %s", str(self.station)
|
"No Weather or HomeCoach devices found for %s", str(self.station)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
except requests.exceptions.Timeout:
|
except (requests.exceptions.Timeout, urllib3.exceptions.ReadTimeoutError):
|
||||||
_LOGGER.warning("Timed out when connecting to Netatmo server.")
|
_LOGGER.warning("Timed out when connecting to Netatmo server.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.station is not None:
|
data = self.station_data.lastData(
|
||||||
data = self.station_data.lastData(station=self.station, exclude=3600)
|
station=self.station, exclude=3600, byId=True
|
||||||
else:
|
)
|
||||||
data = self.station_data.lastData(exclude=3600)
|
|
||||||
if not data:
|
if not data:
|
||||||
self._next_update = time() + NETATMO_UPDATE_INTERVAL
|
self._next_update = time() + NETATMO_UPDATE_INTERVAL
|
||||||
return
|
return
|
||||||
|
@ -1102,7 +1102,7 @@ pyalmond==0.0.2
|
|||||||
pyarlo==0.2.3
|
pyarlo==0.2.3
|
||||||
|
|
||||||
# homeassistant.components.netatmo
|
# homeassistant.components.netatmo
|
||||||
pyatmo==2.3.2
|
pyatmo==2.3.3
|
||||||
|
|
||||||
# homeassistant.components.atome
|
# homeassistant.components.atome
|
||||||
pyatome==0.1.1
|
pyatome==0.1.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user