Fix sensor type creation with multiple Ambient weather stations (#30850)

This commit is contained in:
Aaron Bach 2020-01-16 12:37:29 -07:00 committed by Paulus Schoutsen
parent 7da84dca76
commit 9bfcd04a4f
4 changed files with 25 additions and 12 deletions

View File

@ -26,6 +26,7 @@ from homeassistant.helpers.event import async_call_later
from .config_flow import configured_instances from .config_flow import configured_instances
from .const import ( from .const import (
ATTR_LAST_DATA, ATTR_LAST_DATA,
ATTR_MONITORED_CONDITIONS,
CONF_APP_KEY, CONF_APP_KEY,
DATA_CLIENT, DATA_CLIENT,
DOMAIN, DOMAIN,
@ -341,7 +342,6 @@ class AmbientStation:
self._watchdog_listener = None self._watchdog_listener = None
self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY
self.client = client self.client = client
self.monitored_conditions = []
self.stations = {} self.stations = {}
async def _attempt_connect(self): async def _attempt_connect(self):
@ -398,19 +398,19 @@ class AmbientStation:
_LOGGER.debug("New station subscription: %s", data) _LOGGER.debug("New station subscription: %s", data)
self.monitored_conditions = [ # Only create entities based on the data coming through the socket.
# If the user is monitoring brightness (in W/m^2), make sure we also
# add a calculated sensor for the same data measured in lx:
monitored_conditions = [
k for k in station["lastData"] if k in SENSOR_TYPES k for k in station["lastData"] if k in SENSOR_TYPES
] ]
if TYPE_SOLARRADIATION in monitored_conditions:
# If the user is monitoring brightness (in W/m^2), monitored_conditions.append(TYPE_SOLARRADIATION_LX)
# make sure we also add a calculated sensor for the
# same data measured in lx:
if TYPE_SOLARRADIATION in self.monitored_conditions:
self.monitored_conditions.append(TYPE_SOLARRADIATION_LX)
self.stations[station["macAddress"]] = { self.stations[station["macAddress"]] = {
ATTR_LAST_DATA: station["lastData"], ATTR_LAST_DATA: station["lastData"],
ATTR_LOCATION: station.get("info", {}).get("location"), ATTR_LOCATION: station.get("info", {}).get("location"),
ATTR_MONITORED_CONDITIONS: monitored_conditions,
ATTR_NAME: station.get("info", {}).get( ATTR_NAME: station.get("info", {}).get(
"name", station["macAddress"] "name", station["macAddress"]
), ),

View File

@ -19,7 +19,13 @@ from . import (
TYPE_BATTOUT, TYPE_BATTOUT,
AmbientWeatherEntity, AmbientWeatherEntity,
) )
from .const import ATTR_LAST_DATA, DATA_CLIENT, DOMAIN, TYPE_BINARY_SENSOR from .const import (
ATTR_LAST_DATA,
ATTR_MONITORED_CONDITIONS,
DATA_CLIENT,
DOMAIN,
TYPE_BINARY_SENSOR,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -35,7 +41,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
binary_sensor_list = [] binary_sensor_list = []
for mac_address, station in ambient.stations.items(): for mac_address, station in ambient.stations.items():
for condition in ambient.monitored_conditions: for condition in station[ATTR_MONITORED_CONDITIONS]:
name, _, kind, device_class = SENSOR_TYPES[condition] name, _, kind, device_class = SENSOR_TYPES[condition]
if kind == TYPE_BINARY_SENSOR: if kind == TYPE_BINARY_SENSOR:
binary_sensor_list.append( binary_sensor_list.append(

View File

@ -2,6 +2,7 @@
DOMAIN = "ambient_station" DOMAIN = "ambient_station"
ATTR_LAST_DATA = "last_data" ATTR_LAST_DATA = "last_data"
ATTR_MONITORED_CONDITIONS = "monitored_conditions"
CONF_APP_KEY = "app_key" CONF_APP_KEY = "app_key"

View File

@ -9,7 +9,13 @@ from . import (
TYPE_SOLARRADIATION_LX, TYPE_SOLARRADIATION_LX,
AmbientWeatherEntity, AmbientWeatherEntity,
) )
from .const import ATTR_LAST_DATA, DATA_CLIENT, DOMAIN, TYPE_SENSOR from .const import (
ATTR_LAST_DATA,
ATTR_MONITORED_CONDITIONS,
DATA_CLIENT,
DOMAIN,
TYPE_SENSOR,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -25,7 +31,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
sensor_list = [] sensor_list = []
for mac_address, station in ambient.stations.items(): for mac_address, station in ambient.stations.items():
for condition in ambient.monitored_conditions: for condition in station[ATTR_MONITORED_CONDITIONS]:
name, unit, kind, device_class = SENSOR_TYPES[condition] name, unit, kind, device_class = SENSOR_TYPES[condition]
if kind == TYPE_SENSOR: if kind == TYPE_SENSOR:
sensor_list.append( sensor_list.append(