Cleanup Netatmo sensors (#38627)

This commit is contained in:
cgtobi 2020-08-13 09:36:47 +02:00 committed by GitHub
parent 9244bf28ef
commit 3957337b9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,9 +6,13 @@ from homeassistant.const import (
ATTR_LATITUDE,
ATTR_LONGITUDE,
CONCENTRATION_PARTS_PER_MILLION,
DEGREE,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_TEMPERATURE,
PRESSURE_MBAR,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
UNIT_PERCENTAGE,
@ -50,7 +54,7 @@ SENSOR_TYPES = {
DEVICE_CLASS_TEMPERATURE,
],
"co2": ["CO2", CONCENTRATION_PARTS_PER_MILLION, "mdi:molecule-co2", None],
"pressure": ["Pressure", "mbar", "mdi:gauge", None],
"pressure": ["Pressure", PRESSURE_MBAR, "mdi:gauge", DEVICE_CLASS_PRESSURE],
"noise": ["Noise", "dB", "mdi:volume-high", None],
"humidity": [
"Humidity",
@ -64,30 +68,40 @@ SENSOR_TYPES = {
"battery_vp": ["Battery", "", "mdi:battery", None],
"battery_lvl": ["Battery Level", "", "mdi:battery", None],
"battery_percent": ["Battery Percent", UNIT_PERCENTAGE, None, DEVICE_CLASS_BATTERY],
"min_temp": ["Min Temp.", TEMP_CELSIUS, "mdi:thermometer", None],
"max_temp": ["Max Temp.", TEMP_CELSIUS, "mdi:thermometer", None],
"windangle": ["Angle", "", "mdi:compass", None],
"windangle_value": ["Angle Value", "º", "mdi:compass", None],
"min_temp": [
"Min Temp.",
TEMP_CELSIUS,
"mdi:thermometer",
DEVICE_CLASS_TEMPERATURE,
],
"max_temp": [
"Max Temp.",
TEMP_CELSIUS,
"mdi:thermometer",
DEVICE_CLASS_TEMPERATURE,
],
"windangle": ["Angle", None, "mdi:compass-outline", None],
"windangle_value": ["Angle Value", DEGREE, "mdi:compass-outline", None],
"windstrength": [
"Wind Strength",
SPEED_KILOMETERS_PER_HOUR,
"mdi:weather-windy",
None,
],
"gustangle": ["Gust Angle", "", "mdi:compass", None],
"gustangle_value": ["Gust Angle Value", "º", "mdi:compass", None],
"gustangle": ["Gust Angle", None, "mdi:compass-outline", None],
"gustangle_value": ["Gust Angle Value", DEGREE, "mdi:compass-outline", None],
"guststrength": [
"Gust Strength",
SPEED_KILOMETERS_PER_HOUR,
"mdi:weather-windy",
None,
],
"reachable": ["Reachability", "", "mdi:signal", None],
"rf_status": ["Radio", "", "mdi:signal", None],
"rf_status_lvl": ["Radio Level", "", "mdi:signal", None],
"wifi_status": ["Wifi", "", "mdi:wifi", None],
"wifi_status_lvl": ["Wifi Level", "dBm", "mdi:wifi", None],
"health_idx": ["Health", "", "mdi:cloud", None],
"reachable": ["Reachability", None, "mdi:signal", None],
"rf_status": ["Radio", None, "mdi:signal", None],
"rf_status_lvl": ["Radio Level", "", "mdi:signal", DEVICE_CLASS_SIGNAL_STRENGTH],
"wifi_status": ["Wifi", None, "mdi:wifi", None],
"wifi_status_lvl": ["Wifi Level", "dBm", "mdi:wifi", DEVICE_CLASS_SIGNAL_STRENGTH],
"health_idx": ["Health", None, "mdi:cloud", None],
}
MODULE_TYPE_OUTDOOR = "NAModule1"
@ -107,7 +121,6 @@ PUBLIC = "public"
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Netatmo weather and homecoach platform."""
device_registry = await hass.helpers.device_registry.async_get_registry()
data_handler = hass.data[DOMAIN][entry.entry_id][DATA_HANDLER]
async def find_entities(data_class_name):
@ -137,13 +150,21 @@ async def async_setup_entry(hass, entry, async_add_entities):
_LOGGER.debug(
"Adding module %s %s", module.get("module_name"), module.get("_id"),
)
for condition in data_class.get_monitored_conditions(
module_id=module["_id"]
):
conditions = [
c.lower()
for c in data_class.get_monitored_conditions(module_id=module["_id"])
]
for condition in conditions:
if f"{condition}_value" in SENSOR_TYPES:
conditions.append(f"{condition}_value")
elif f"{condition}_lvl" in SENSOR_TYPES:
conditions.append(f"{condition}_lvl")
elif condition == "battery_vp":
conditions.append("battery_lvl")
for condition in conditions:
entities.append(
NetatmoSensor(
data_handler, data_class_name, module, condition.lower()
)
NetatmoSensor(data_handler, data_class_name, module, condition)
)
return entities
@ -154,6 +175,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
]:
async_add_entities(await find_entities(data_class_name), True)
device_registry = await hass.helpers.device_registry.async_get_registry()
@callback
async def add_public_entities(update=True):
"""Retrieve Netatmo public weather entities."""
@ -214,11 +237,6 @@ async def async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) ->
async_dispatcher_send(hass, f"signal-{DOMAIN}-public-update-{entry.entry_id}")
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Netatmo weather and homecoach platform."""
return
class NetatmoSensor(NetatmoBase):
"""Implementation of a Netatmo sensor."""
@ -298,8 +316,8 @@ class NetatmoSensor(NetatmoBase):
if data is None:
if self._state:
_LOGGER.debug(
"No data (%s) found for %s (%s)",
self._data,
"No data found for %s - %s (%s)",
self.name,
self._device_name,
self._id,
)
@ -367,22 +385,22 @@ class NetatmoSensor(NetatmoBase):
def process_angle(angle: int) -> str:
"""Process angle and return string for display."""
if angle >= 330:
return f"N ({angle}\xb0)"
return "N"
if angle >= 300:
return f"NW ({angle}\xb0)"
return "NW"
if angle >= 240:
return f"W ({angle}\xb0)"
return "W"
if angle >= 210:
return f"SW ({angle}\xb0)"
return "SW"
if angle >= 150:
return f"S ({angle}\xb0)"
return "S"
if angle >= 120:
return f"SE ({angle}\xb0)"
return "SE"
if angle >= 60:
return f"E ({angle}\xb0)"
return "E"
if angle >= 30:
return f"NE ({angle}\xb0)"
return f"N ({angle}\xb0)"
return "NE"
return "N"
def process_battery(data: int, model: str) -> str:
@ -524,7 +542,6 @@ class NetatmoPublicSensor(NetatmoBase):
)
)
@callback
async def async_config_update_callback(self, area):
"""Update the entity's config."""
if self.area == area: