2019-02-13 21:21:14 +01:00
|
|
|
"""Support the sensor of a BloomSky weather station."""
|
2016-09-02 06:31:32 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 12:37:16 +01:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2020-02-28 20:46:48 +01:00
|
|
|
from homeassistant.const import (
|
2020-09-15 23:00:26 +02:00
|
|
|
AREA_SQUARE_METERS,
|
2020-02-28 20:46:48 +01:00
|
|
|
CONF_MONITORED_CONDITIONS,
|
2020-09-05 21:09:14 +02:00
|
|
|
PERCENTAGE,
|
2020-09-19 09:26:08 +02:00
|
|
|
PRESSURE_INHG,
|
|
|
|
PRESSURE_MBAR,
|
2020-02-28 20:46:48 +01:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2016-09-02 06:31:32 +02:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2020-04-05 23:16:30 +02:00
|
|
|
from . import DOMAIN
|
2019-04-11 23:37:45 -07:00
|
|
|
|
2016-02-06 08:23:30 +01:00
|
|
|
# These are the available sensors
|
2019-07-31 12:25:30 -07:00
|
|
|
SENSOR_TYPES = [
|
|
|
|
"Temperature",
|
|
|
|
"Humidity",
|
|
|
|
"Pressure",
|
|
|
|
"Luminance",
|
|
|
|
"UVIndex",
|
|
|
|
"Voltage",
|
|
|
|
]
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2016-02-06 08:23:30 +01:00
|
|
|
# Sensor units - these do not currently align with the API documentation
|
2019-07-31 12:25:30 -07:00
|
|
|
SENSOR_UNITS_IMPERIAL = {
|
|
|
|
"Temperature": TEMP_FAHRENHEIT,
|
2020-09-05 21:09:14 +02:00
|
|
|
"Humidity": PERCENTAGE,
|
2020-09-19 09:26:08 +02:00
|
|
|
"Pressure": PRESSURE_INHG,
|
2020-09-15 23:00:26 +02:00
|
|
|
"Luminance": f"cd/{AREA_SQUARE_METERS}",
|
2019-07-31 12:25:30 -07:00
|
|
|
"Voltage": "mV",
|
|
|
|
}
|
2019-07-24 19:37:36 +02:00
|
|
|
|
|
|
|
# Metric units
|
2019-07-31 12:25:30 -07:00
|
|
|
SENSOR_UNITS_METRIC = {
|
|
|
|
"Temperature": TEMP_CELSIUS,
|
2020-09-05 21:09:14 +02:00
|
|
|
"Humidity": PERCENTAGE,
|
2020-09-19 09:26:08 +02:00
|
|
|
"Pressure": PRESSURE_MBAR,
|
2020-09-15 23:00:26 +02:00
|
|
|
"Luminance": f"cd/{AREA_SQUARE_METERS}",
|
2019-07-31 12:25:30 -07:00
|
|
|
"Voltage": "mV",
|
|
|
|
}
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2016-02-06 08:23:30 +01:00
|
|
|
# Which sensors to format numerically
|
2019-07-31 12:25:30 -07:00
|
|
|
FORMAT_NUMBERS = ["Temperature", "Pressure", "Voltage"]
|
2016-09-02 06:31:32 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_TYPES): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the available BloomSky weather sensors."""
|
2016-09-10 09:12:24 -07:00
|
|
|
# Default needed in case of discovery
|
2020-04-24 01:00:57 +02:00
|
|
|
if discovery_info is not None:
|
|
|
|
return
|
|
|
|
|
2020-04-09 09:26:06 +02:00
|
|
|
sensors = config[CONF_MONITORED_CONDITIONS]
|
2020-04-05 23:16:30 +02:00
|
|
|
bloomsky = hass.data[DOMAIN]
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2020-04-05 23:16:30 +02:00
|
|
|
for device in bloomsky.devices.values():
|
2016-02-19 23:21:56 -08:00
|
|
|
for variable in sensors:
|
2020-04-05 23:16:30 +02:00
|
|
|
add_entities([BloomSkySensor(bloomsky, device, variable)], True)
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
|
2021-03-22 12:37:16 +01:00
|
|
|
class BloomSkySensor(SensorEntity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a single sensor in a BloomSky device."""
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
def __init__(self, bs, device, sensor_name):
|
2016-09-02 06:31:32 +02:00
|
|
|
"""Initialize a BloomSky sensor."""
|
2016-02-04 20:01:45 +00:00
|
|
|
self._bloomsky = bs
|
2019-07-31 12:25:30 -07:00
|
|
|
self._device_id = device["DeviceID"]
|
2016-02-04 20:01:45 +00:00
|
|
|
self._sensor_name = sensor_name
|
2020-02-24 17:47:52 +01:00
|
|
|
self._name = f"{device['DeviceName']} {sensor_name}"
|
2017-08-06 19:21:55 +02:00
|
|
|
self._state = None
|
2019-09-03 17:09:59 +02:00
|
|
|
self._unique_id = f"{self._device_id}-{self._sensor_name}"
|
2018-10-13 01:23:00 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Return the name of the BloomSky device and this sensor."""
|
2016-02-19 23:21:56 -08:00
|
|
|
return self._name
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Return the current state, eg. value, of this sensor."""
|
2016-02-04 20:01:45 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-02-19 23:21:56 -08:00
|
|
|
"""Return the sensor units."""
|
2019-07-24 19:37:36 +02:00
|
|
|
if self._bloomsky.is_metric:
|
|
|
|
return SENSOR_UNITS_METRIC.get(self._sensor_name, None)
|
|
|
|
return SENSOR_UNITS_IMPERIAL.get(self._sensor_name, None)
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-02-19 23:21:56 -08:00
|
|
|
"""Request an update from the BloomSky API."""
|
2016-02-04 20:01:45 +00:00
|
|
|
self._bloomsky.refresh_devices()
|
2016-02-19 23:21:56 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
state = self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]
|
2016-02-19 23:21:56 -08:00
|
|
|
|
|
|
|
if self._sensor_name in FORMAT_NUMBERS:
|
2019-09-03 17:09:59 +02:00
|
|
|
self._state = f"{state:.2f}"
|
2016-02-04 20:01:45 +00:00
|
|
|
else:
|
2016-02-19 23:21:56 -08:00
|
|
|
self._state = state
|