mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-11 22:34:04 +02:00
109 lines
3.2 KiB
Python
Executable file
109 lines
3.2 KiB
Python
Executable file
"""
|
|
Circadian Lighting Sensor for Home-Assistant.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from homeassistant.helpers.dispatcher import dispatcher_connect
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from custom_components.circadian_lighting import (
|
|
CIRCADIAN_LIGHTING_UPDATE_TOPIC,
|
|
DOMAIN,
|
|
log,
|
|
)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
ICON = "mdi:theme-light-dark"
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
"""Set up the Circadian Lighting sensor."""
|
|
circadian_lighting = hass.data.get(DOMAIN)
|
|
if circadian_lighting is not None:
|
|
sensor = CircadianSensor(hass, circadian_lighting)
|
|
add_devices([sensor])
|
|
|
|
def update(call=None):
|
|
"""Update component."""
|
|
circadian_lighting._update()
|
|
|
|
service_name = "values_update"
|
|
hass.services.register(DOMAIN, service_name, update)
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
class CircadianSensor(Entity):
|
|
"""Representation of a Circadian Lighting sensor."""
|
|
|
|
def __init__(self, hass, circadian_lighting):
|
|
"""Initialize the Circadian Lighting sensor."""
|
|
self._circadian_lighting = circadian_lighting
|
|
self._name = "Circadian Values"
|
|
self._entity_id = "sensor.circadian_values"
|
|
self._state = self._circadian_lighting._percent
|
|
self._unit_of_measurement = "%"
|
|
self._icon = ICON
|
|
self._hs_color = self._circadian_lighting._hs_color
|
|
self._colortemp = self._circadian_lighting._colortemp
|
|
self._rgb_color = self._circadian_lighting._rgb_color
|
|
self._xy_color = self._circadian_lighting._xy_color
|
|
|
|
# Register callbacks
|
|
dispatcher_connect(hass, CIRCADIAN_LIGHTING_UPDATE_TOPIC, self.update_sensor)
|
|
|
|
@property
|
|
def entity_id(self):
|
|
"""Return the entity ID of the sensor."""
|
|
return self._entity_id
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the sensor."""
|
|
return 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 self._unit_of_measurement
|
|
|
|
@property
|
|
def icon(self):
|
|
"""Icon to use in the frontend, if any."""
|
|
return self._icon
|
|
|
|
@property
|
|
def hs_color(self):
|
|
return self._hs_color
|
|
|
|
@property
|
|
def device_state_attributes(self):
|
|
"""Return the attributes of the sensor."""
|
|
return {
|
|
"colortemp": self._circadian_lighting._colortemp,
|
|
"rgb_color": self._circadian_lighting._rgb_color,
|
|
"xy_color": self._circadian_lighting._xy_color,
|
|
}
|
|
|
|
def update(self):
|
|
"""Fetch new state data for the sensor.
|
|
|
|
This is the only method that should fetch new data for Home Assistant.
|
|
"""
|
|
self._circadian_lighting.update()
|
|
|
|
@log(logger=_LOGGER)
|
|
def update_sensor(self):
|
|
self._state = self._circadian_lighting._percent
|
|
self._hs_color = self._circadian_lighting._hs_color
|
|
self._colortemp = self._circadian_lighting._colortemp
|
|
self._rgb_color = self._circadian_lighting._rgb_color
|
|
self._xy_color = self._circadian_lighting._xy_color
|