Skip to content

Commit

Permalink
Add an optional poll mode
Browse files Browse the repository at this point in the history
This adds an optional poll mode wich can be enabled through a platform
configuration variable in yaml. This is implemented to avoid issues such
as #5
  • Loading branch information
rautesamtr committed Jan 15, 2022
1 parent 4b9e26f commit f21acfe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -19,6 +19,8 @@ To use, add the following to your `configuration.yaml` file:
```yaml
sensor:
- platform: thermal_comfort
poll: true
scan_interval: 300
sensors:
livingroom:
friendly_name: Living Room
Expand All @@ -33,7 +35,15 @@ sensor:
bedroom:
```
### Configuration Variables
### Platform Configuration Variables
<dl>
<dt><strong>poll</strong> <code>boolean</code> <code>(optional, default: false)</code></dt>
<dd>Set to true if you want the sensors to be polled. This can avoid double calculated values if your input sensors split change updates for humidity and temperature.</dd>
<dt><strong>scan_interval</strong> <code>boolean</code> <code>(optional, default: 30)</code></dt>
<dd>Change the polling interval in seconds if <code>poll</code> is set to true.</dd>
</dl>

### Sensor Configuration Variables
<dl>
<dt><strong>temperature_sensor</strong> <code>string</code> <code>REQUIRED</code></dt>
<dd>ID of temperature sensor entity to be used for calculations.</dd>
Expand Down
24 changes: 15 additions & 9 deletions custom_components/thermal_comfort/sensor.py
Expand Up @@ -38,11 +38,12 @@

_LOGGER = logging.getLogger(__name__)

CONF_TEMPERATURE_SENSOR = 'temperature_sensor'
CONF_HUMIDITY_SENSOR = 'humidity_sensor'
CONF_SENSOR_TYPES = 'sensor_types'
ATTR_HUMIDITY = 'humidity'
ATTR_FROST_RISK_LEVEL = 'frost_risk_level'
CONF_HUMIDITY_SENSOR = 'humidity_sensor'
CONF_POLL = 'poll'
CONF_SENSOR_TYPES = 'sensor_types'
CONF_TEMPERATURE_SENSOR = 'temperature_sensor'

class ThermalComfortDeviceClass(StrEnum):
"""State class for thermal comfort sensors."""
Expand Down Expand Up @@ -128,6 +129,7 @@ class SensorType(StrEnum):

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SENSORS): cv.schema_with_slug_keys(SENSOR_SCHEMA),
vol.Optional(CONF_POLL, default=False): cv.boolean,
})

class ThermalPerception(StrEnum):
Expand Down Expand Up @@ -179,6 +181,7 @@ async def async_setup_platform(hass, config, async_add_entities,
"""Set up the Thermal Comfort sensors."""
sensors = []

should_poll = config.get(CONF_POLL)
for device, device_config in config[CONF_SENSORS].items():
temperature_entity = device_config.get(CONF_TEMPERATURE_SENSOR)
humidity_entity = device_config.get(CONF_HUMIDITY_SENSOR)
Expand All @@ -193,6 +196,7 @@ async def async_setup_platform(hass, config, async_add_entities,
temperature_entity,
humidity_entity,
sensor_types,
should_poll,
)

for sensor_type in sensor_types:
Expand Down Expand Up @@ -233,15 +237,15 @@ def __init__(self, device, device_id, friendly_name, entity_description, icon_te
self._attr_extra_state_attributes = {}
if unique_id is not None:
self._attr_unique_id = unique_id + sensor_type
self._attr_should_poll = False
self._attr_should_poll = self._device.should_poll

@property
def extra_state_attributes(self):
return dict(self._device.extra_state_attributes, **self._attr_extra_state_attributes)

async def async_added_to_hass(self):
"""Register callbacks."""
self._device._sensors.append(self)
self._device.sensors.append(self)

async def async_update(self):
"""Update the state of the sensor."""
Expand Down Expand Up @@ -291,7 +295,8 @@ def __init__(self,
hass,
temperature_entity,
humidity_entity,
sensor_types
sensor_types,
should_poll,
):
"""Initialize the sensor."""
self.hass = hass
Expand All @@ -301,7 +306,8 @@ def __init__(self,
self._temperature = None
self._humidity = None
self._sensor_types = sensor_types
self._sensors = []
self.should_poll = should_poll
self.sensors = []
self._compute_states = { sensor_type: ComputeState(lock=Lock()) for sensor_type in SENSOR_TYPES.keys() }

temperature_state = hass.states.get(temperature_entity)
Expand Down Expand Up @@ -471,8 +477,8 @@ async def async_update(self):
if self._temperature is not None and self._humidity is not None:
for sensor_type in SENSOR_TYPES.keys():
self._compute_states[sensor_type].needs_update = True
for sensor in self._sensors:
if sensor is not None:
if not self.should_poll:
for sensor in self.sensors:
sensor.async_schedule_update_ha_state(True)


Expand Down

0 comments on commit f21acfe

Please sign in to comment.