def publish_split_topic_dsmr_reading(reading: DsmrReading) -> None: """ Publishes a DSMR reading to a broker, formatted in a separate topic per field name, if set and enabled. """ split_topic_settings = telegram.SplitTopicTelegramMQTTSettings.get_solo() if not split_topic_settings.enabled: return # Default to UTC, but allow local timezone on demand (#463). if split_topic_settings.use_local_timezone: reading.convert_to_local_timezone() publish_split_topic_data(mapping_format=split_topic_settings.formatting, data_source=reading)
def publish_json_dsmr_reading(reading: DsmrReading) -> None: """ Publishes a JSON formatted DSMR reading to a broker, if set and enabled. """ json_settings = telegram.JSONTelegramMQTTSettings.get_solo() if not json_settings.enabled: return # Default to UTC, but allow local timezone on demand (#463). if json_settings.use_local_timezone: reading.convert_to_local_timezone() publish_json_data(topic=json_settings.topic, mapping_format=json_settings.formatting, data_source=reading)
def compact(dsmr_reading: DsmrReading) -> None: """ Compacts/converts DSMR readings to consumption data. Optionally groups electricity by minute. """ consumption_settings = ConsumptionSettings.get_solo() # Grouping by minute requires some distinction and history checking. reading_start = timezone.datetime.combine( dsmr_reading.timestamp.date(), time(hour=dsmr_reading.timestamp.hour, minute=dsmr_reading.timestamp.minute), ).replace(tzinfo=pytz.UTC) if consumption_settings.electricity_grouping_type == ConsumptionSettings.ELECTRICITY_GROUPING_BY_MINUTE: system_time_past_minute = timezone.now( ) >= reading_start + timezone.timedelta(minutes=1) reading_past_minute_exists = DsmrReading.objects.filter( timestamp__gte=reading_start + timezone.timedelta(minutes=1)).exists() # Postpone until the minute has passed on the system time. And when there are (new) readings beyond this minute. if not system_time_past_minute or not reading_past_minute_exists: logger.debug( 'Compact: Waiting for newer readings before grouping data...') raise CompactorNotReadyError() # Create consumption records. _compact_electricity(dsmr_reading=dsmr_reading, electricity_grouping_type=consumption_settings. electricity_grouping_type, reading_start=reading_start) _compact_gas(dsmr_reading=dsmr_reading, gas_grouping_type=consumption_settings.gas_grouping_type) dsmr_reading.processed = True dsmr_reading.save(update_fields=['processed']) logger.debug('Compact: Processed reading: %s', dsmr_reading)
def test_ordering(self): """ Test whether default model sorting is as expected. """ self.assertEqual(DsmrReading()._meta.ordering, ['timestamp'])