2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ambient Weather Station sensors."""
|
2019-01-28 23:35:39 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.const import ATTR_NAME
|
|
|
|
|
2019-06-22 05:12:16 +00:00
|
|
|
from . import SENSOR_TYPES, TYPE_SOLARRADIATION, AmbientWeatherEntity
|
2019-02-07 21:35:23 +00:00
|
|
|
from .const import ATTR_LAST_DATA, DATA_CLIENT, DOMAIN, TYPE_SENSOR
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2019-02-07 21:35:23 +00:00
|
|
|
"""Set up Ambient PWS sensors based on existing config."""
|
2019-01-28 23:35:39 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
2019-02-07 21:35:23 +00:00
|
|
|
"""Set up Ambient PWS sensors based on a config entry."""
|
2019-01-28 23:35:39 +00:00
|
|
|
ambient = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
|
|
|
|
|
|
|
sensor_list = []
|
|
|
|
for mac_address, station in ambient.stations.items():
|
|
|
|
for condition in ambient.monitored_conditions:
|
2019-06-21 16:27:53 +00:00
|
|
|
name, unit, kind, device_class = SENSOR_TYPES[condition]
|
2019-02-07 21:35:23 +00:00
|
|
|
if kind == TYPE_SENSOR:
|
|
|
|
sensor_list.append(
|
|
|
|
AmbientWeatherSensor(
|
|
|
|
ambient, mac_address, station[ATTR_NAME], condition,
|
2019-06-21 16:27:53 +00:00
|
|
|
name, device_class, unit))
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
async_add_entities(sensor_list, True)
|
|
|
|
|
|
|
|
|
2019-02-07 21:35:23 +00:00
|
|
|
class AmbientWeatherSensor(AmbientWeatherEntity):
|
2019-01-28 23:35:39 +00:00
|
|
|
"""Define an Ambient sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, ambient, mac_address, station_name, sensor_type, sensor_name,
|
2019-06-21 16:27:53 +00:00
|
|
|
device_class, unit):
|
2019-01-28 23:35:39 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-02-07 21:35:23 +00:00
|
|
|
super().__init__(
|
2019-06-21 16:27:53 +00:00
|
|
|
ambient,
|
|
|
|
mac_address,
|
|
|
|
station_name,
|
|
|
|
sensor_type,
|
|
|
|
sensor_name,
|
|
|
|
device_class)
|
2019-01-28 23:35:39 +00:00
|
|
|
|
2019-02-07 21:35:23 +00:00
|
|
|
self._unit = unit
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
2019-02-03 23:23:30 +00:00
|
|
|
return self._unit
|
2019-01-28 23:35:39 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the sensor."""
|
2019-06-22 05:12:16 +00:00
|
|
|
new_state = self._ambient.stations[
|
2019-01-28 23:35:39 +00:00
|
|
|
self._mac_address][ATTR_LAST_DATA].get(self._sensor_type)
|
2019-06-22 05:12:16 +00:00
|
|
|
|
|
|
|
if self._sensor_type == TYPE_SOLARRADIATION:
|
|
|
|
# Ambient's units for solar radiation (illuminance) are
|
|
|
|
# W/m^2; since those aren't commonly used in the HASS
|
|
|
|
# world, transform them to lx:
|
|
|
|
self._state = round(float(new_state)/0.0079)
|
|
|
|
else:
|
|
|
|
self._state = new_state
|