core/homeassistant/components/bloomsky/sensor.py

120 lines
3.5 KiB
Python
Raw Normal View History

"""Support the sensor of a BloomSky weather station."""
from __future__ import annotations
2016-09-02 04:31:32 +00:00
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.const import (
AREA_SQUARE_METERS,
CONF_MONITORED_CONDITIONS,
PERCENTAGE,
UnitOfElectricPotential,
UnitOfPressure,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
2016-09-02 04:31:32 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2016-09-02 04:31:32 +00:00
from . import DOMAIN
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": UnitOfTemperature.FAHRENHEIT,
"Humidity": PERCENTAGE,
"Pressure": UnitOfPressure.INHG,
"Luminance": f"cd/{AREA_SQUARE_METERS}",
"Voltage": UnitOfElectricPotential.MILLIVOLT,
2019-07-31 19:25:30 +00:00
}
# Metric units
2019-07-31 19:25:30 +00:00
SENSOR_UNITS_METRIC = {
"Temperature": UnitOfTemperature.CELSIUS,
"Humidity": PERCENTAGE,
"Pressure": UnitOfPressure.MBAR,
"Luminance": f"cd/{AREA_SQUARE_METERS}",
"Voltage": UnitOfElectricPotential.MILLIVOLT,
2019-07-31 19:25:30 +00:00
}
2016-02-04 20:01:45 +00:00
# Device class
SENSOR_DEVICE_CLASS = {
"Temperature": SensorDeviceClass.TEMPERATURE,
"Humidity": SensorDeviceClass.HUMIDITY,
"Pressure": SensorDeviceClass.PRESSURE,
"Voltage": SensorDeviceClass.VOLTAGE,
}
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: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the available BloomSky weather sensors."""
# Default needed in case of discovery
if discovery_info is not None:
return
sensors = config[CONF_MONITORED_CONDITIONS]
bloomsky = hass.data[DOMAIN]
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:
add_entities([BloomSkySensor(bloomsky, device, variable)], True)
2016-02-04 20:01:45 +00:00
class BloomSkySensor(SensorEntity):
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
self._attr_name = f"{device['DeviceName']} {sensor_name}"
self._attr_unique_id = f"{self._device_id}-{sensor_name}"
self._attr_device_class = SENSOR_DEVICE_CLASS.get(sensor_name)
self._attr_native_unit_of_measurement = SENSOR_UNITS_IMPERIAL.get(
sensor_name, None
)
if self._bloomsky.is_metric:
self._attr_native_unit_of_measurement = SENSOR_UNITS_METRIC.get(
sensor_name, None
)
2016-02-04 20:01:45 +00:00
2022-08-19 07:54:13 +00:00
def update(self) -> None:
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()
2019-07-31 19:25:30 +00:00
state = self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]
self._attr_native_value = (
f"{state:.2f}" if self._sensor_name in FORMAT_NUMBERS else state
)