from caspia.gateway.services import GatewayService
from caspia.meadow.client import characteristic_read_handler, characteristic_write_handler
from caspia.meadow.errors import SensorFailureError
from caspia.meadow.services import CarbonDioxideSensorBase, Characteristic
from caspia.node.components.sensors.scd30 import SCD30Sensor
[docs]class SCD30CarbonDioxideSensor(GatewayService, CarbonDioxideSensorBase):
auto_discovery = False
self_calibration_enabled = Characteristic('boolean', 'W')
@property
def is_updated_periodically(self):
return self.config['scd30'].interval != 0
@property
def dependant_components(self):
return {self.scd30_c}
[docs] async def on_component_event(self, component, event):
if self.scd30_c == component and isinstance(event, SCD30Sensor.MeasurementEvent):
if not event.error:
value = int(event.co2)
else:
value = SensorFailureError('sensor reported an error')
await self.notify(self.level, value)
[docs] @characteristic_read_handler('level')
async def level_read(self, **kwargs):
if not self.is_updated_periodically:
await self.scd30_c.measure()
if self.scd30_c.state.error:
raise SensorFailureError('sensor reported an error')
return int(self.scd30_c.state.co2)
[docs] @characteristic_write_handler('self_calibration_enabled')
async def self_calibration_enabled_write(self, value, **kwargs):
await self.scd30_c.set_self_calibration_enabled(value)