core/homeassistant/components/bloomsky/sensor.py

109 lines
3.0 KiB
Python
Raw Normal View History

"""Support the sensor of a BloomSky weather station."""
2016-02-04 20:01:45 +00:00
import logging
2016-02-19 05:27:50 +00:00
2016-09-02 04:31:32 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
2019-07-31 19:25:30 +00:00
from homeassistant.const import TEMP_FAHRENHEIT, TEMP_CELSIUS, CONF_MONITORED_CONDITIONS
2016-02-04 20:01:45 +00:00
from homeassistant.helpers.entity import Entity
2016-09-02 04:31:32 +00:00
import homeassistant.helpers.config_validation as cv
from . import BLOOMSKY
LOGGER = logging.getLogger(__name__)
2016-02-04 20:01:45 +00:00
2016-02-06 07:23:30 +00:00
# These are the available sensors
2019-07-31 19:25:30 +00:00
SENSOR_TYPES = [
"Temperature",
"Humidity",
"Pressure",
"Luminance",
"UVIndex",
"Voltage",
]
2016-02-04 20:01:45 +00:00
2016-02-06 07:23:30 +00:00
# Sensor units - these do not currently align with the API documentation
2019-07-31 19:25:30 +00:00
SENSOR_UNITS_IMPERIAL = {
"Temperature": TEMP_FAHRENHEIT,
"Humidity": "%",
"Pressure": "inHg",
"Luminance": "cd/m²",
"Voltage": "mV",
}
# Metric units
2019-07-31 19:25:30 +00:00
SENSOR_UNITS_METRIC = {
"Temperature": TEMP_CELSIUS,
"Humidity": "%",
"Pressure": "mbar",
"Luminance": "cd/m²",
"Voltage": "mV",
}
2016-02-04 20:01:45 +00:00
2016-02-06 07:23:30 +00:00
# Which sensors to format numerically
2019-07-31 19:25:30 +00:00
FORMAT_NUMBERS = ["Temperature", "Pressure", "Voltage"]
2016-09-02 04:31:32 +00:00
2019-07-31 19:25:30 +00: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
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available BloomSky weather sensors."""
# Default needed in case of discovery
sensors = config.get(CONF_MONITORED_CONDITIONS, SENSOR_TYPES)
2016-02-04 20:01:45 +00:00
for device in BLOOMSKY.devices.values():
2016-02-20 07:21:56 +00:00
for variable in sensors:
2019-07-31 19:25:30 +00:00
add_entities([BloomSkySensor(BLOOMSKY, device, variable)], True)
2016-02-04 20:01:45 +00:00
class BloomSkySensor(Entity):
2016-03-08 15:46:34 +00: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 04:31:32 +00:00
"""Initialize a BloomSky sensor."""
2016-02-04 20:01:45 +00:00
self._bloomsky = bs
2019-07-31 19:25:30 +00:00
self._device_id = device["DeviceID"]
2016-02-04 20:01:45 +00:00
self._sensor_name = sensor_name
2019-07-31 19:25:30 +00:00
self._name = "{} {}".format(device["DeviceName"], sensor_name)
self._state = None
self._unique_id = f"{self._device_id}-{self._sensor_name}"
@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):
"""Return the name of the BloomSky device and this sensor."""
2016-02-20 07:21:56 +00:00
return self._name
2016-02-04 20:01:45 +00:00
@property
def state(self):
"""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-20 07:21:56 +00:00
"""Return the sensor units."""
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-20 07:21:56 +00:00
"""Request an update from the BloomSky API."""
2016-02-04 20:01:45 +00:00
self._bloomsky.refresh_devices()
2016-02-20 07:21:56 +00:00
2019-07-31 19:25:30 +00:00
state = self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]
2016-02-20 07:21:56 +00:00
if self._sensor_name in FORMAT_NUMBERS:
self._state = f"{state:.2f}"
2016-02-04 20:01:45 +00:00
else:
2016-02-20 07:21:56 +00:00
self._state = state