Fix netatmo weatherstation setup error (#24788)

* Check if station data exists and reduce calls

* Fix module names list

* Add warning

* Remove dead code
This commit is contained in:
cgtobi 2019-06-28 05:16:46 +02:00 committed by Paulus Schoutsen
parent 41dd70f644
commit a69a00785f

View File

@ -99,6 +99,12 @@ MODULE_TYPE_RAIN = 'NAModule3'
MODULE_TYPE_INDOOR = 'NAModule4' MODULE_TYPE_INDOOR = 'NAModule4'
NETATMO_DEVICE_TYPES = {
'WeatherStationData': 'weather station',
'HomeCoachData': 'home coach'
}
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available Netatmo weather sensors.""" """Set up the available Netatmo weather sensors."""
dev = [] dev = []
@ -132,7 +138,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
import pyatmo import pyatmo
for data_class in [pyatmo.WeatherStationData, pyatmo.HomeCoachData]: for data_class in [pyatmo.WeatherStationData, pyatmo.HomeCoachData]:
try:
data = NetatmoData(auth, data_class, config.get(CONF_STATION)) data = NetatmoData(auth, data_class, config.get(CONF_STATION))
except pyatmo.NoDevice:
_LOGGER.warning(
"No %s devices found",
NETATMO_DEVICE_TYPES[data_class.__name__]
)
continue
# Test if manually configured # Test if manually configured
if CONF_MODULES in config: if CONF_MODULES in config:
module_items = config[CONF_MODULES].items() module_items = config[CONF_MODULES].items()
@ -157,18 +170,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
def find_devices(data): def find_devices(data):
"""Find all devices.""" """Find all devices."""
dev = [] dev = []
not_handled = [] module_names = data.get_module_names()
for module_name in data.get_module_names(): for module_name in module_names:
if (module_name not in data.get_module_names()
and module_name not in not_handled):
not_handled.append(not_handled)
continue
for condition in data.station_data.monitoredConditions(module_name): for condition in data.station_data.monitoredConditions(module_name):
dev.append(NetatmoSensor( dev.append(NetatmoSensor(
data, module_name, condition.lower(), data.station)) data, module_name, condition.lower(), data.station))
for module_name in not_handled:
_LOGGER.error('Module name: "%s" not found', module_name)
return dev return dev
@ -187,12 +193,11 @@ class NetatmoSensor(Entity):
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]
self._module_type = self.netatmo_data. \ module = self.netatmo_data.station_data.moduleByName(
station_data.moduleByName(module=module_name)['type'] station=self.station_name, module=module_name
module_id = self.netatmo_data. \ )
station_data.moduleByName(station=self.station_name, self._module_type = module['type']
module=module_name)['_id'] self._unique_id = '{}-{}'.format(module['_id'], self.type)
self._unique_id = '{}-{}'.format(module_id, self.type)
@property @property
def name(self): def name(self):
@ -515,15 +520,14 @@ class NetatmoData:
self.auth = auth self.auth = auth
self.data_class = data_class self.data_class = data_class
self.data = {} self.data = {}
self.station_data = None self.station_data = self.data_class(self.auth)
self.station = station self.station = station
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_names(self):
"""Return all module available on the API as a list.""" """Return all module available on the API as a list."""
self.update() return self.station_data.modulesNamesList()
return self.data.keys()
def update(self): def update(self):
"""Call the Netatmo API to update the data. """Call the Netatmo API to update the data.