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