ha-kumisensors/sensor.py

198 lines
5.3 KiB
Python

from datetime import timedelta
import logging
import re
from typing import Any, Callable, Dict, Optional
from homeassistant import config_entries, core
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
TEMP_CELSIUS,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_HUMIDITY,
PERCENTAGE,
DEVICE_CLASS_PRESSURE,
PRESSURE_HPA
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
HomeAssistantType,
)
from .const import DOMAIN
from .kumisensors import KumiSensors
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(minutes=5)
async def async_setup_entry(
hass: core.HomeAssistant,
config_entry: config_entries.ConfigEntry,
async_add_entities,
):
"""Setup sensors from a config entry created in the integrations UI."""
config = hass.data[DOMAIN][config_entry.entry_id]
if config_entry.options:
config.update(config_entry.options)
sensors = [KumiTemperatureSensor(config, hass), KumiHumiditySensor(config, hass), KumiPressureSensor(config, hass)]
async_add_entities(sensors, update_before_add=True)
async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable,
discovery_info: Optional[DiscoveryInfoType] = None,
) -> None:
"""Set up the sensor platform."""
sensors = [KumiTemperatureSensor(config, hass), KumiHumiditySensor(config, hass), KumiPressureSensor(config, hass)]
async_add_entities(sensors, update_before_add=True)
class KumiTemperatureSensor(Entity):
"""Representation of a Sensor."""
def __init__(self, config, hass):
"""Initialize the sensor."""
self._sensors = KumiSensors(config[CONF_HOST], hass)
self._name = config.get(CONF_NAME, "Sensors")
self._state = None
@property
def device_info(self):
return {
"identifiers": {
(DOMAIN, self._name)
},
"name": self._name
}
@property
def name(self):
"""Return the name of the sensor."""
return '%s Temperature' % self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def device_class(self):
return DEVICE_CLASS_TEMPERATURE
@property
def unique_id(self):
return "sensor.kumisensors.%s.temperature" % self._name.lower()
async def async_update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = await self._sensors.get_temperature()
class KumiHumiditySensor(Entity):
"""Representation of a Sensor."""
def __init__(self, config, hass):
"""Initialize the sensor."""
self._sensors = KumiSensors(config[CONF_HOST], hass)
self._name = config.get(CONF_NAME, "Sensors")
self._state = None
@property
def device_info(self):
return {
"identifiers": {
(DOMAIN, self._name)
},
"name": self._name
}
@property
def name(self):
"""Return the name of the sensor."""
return '%s Humidity' % self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return PERCENTAGE
@property
def device_class(self):
return DEVICE_CLASS_HUMIDITY
@property
def unique_id(self):
return "sensor.kumisensors.%s.humidity" % self._name.lower()
async def async_update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = await self._sensors.get_humidity()
class KumiPressureSensor(Entity):
"""Representation of a Sensor."""
def __init__(self, config, hass):
"""Initialize the sensor."""
self._sensors = KumiSensors(config[CONF_HOST], hass)
self._name = config.get(CONF_NAME, "Sensors")
self._state = None
@property
def device_info(self):
return {
"identifiers": {
(DOMAIN, self._name)
},
"name": self._name
}
@property
def name(self):
"""Return the name of the sensor."""
return '%s Pressure' % self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return PRESSURE_HPA
@property
def device_class(self):
return DEVICE_CLASS_PRESSURE
@property
def unique_id(self):
return "sensor.kumisensors.%s.pressure" % self._name.lower()
async def async_update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = await self._sensors.get_pressure()