Skip to content

feat: Add state_class to CollectionShipment sensor for long-term statistics#74

Open
robbybarnes wants to merge 2 commits intojmdevita:mainfrom
robbybarnes:feature/add-state-class-to-sensors
Open

feat: Add state_class to CollectionShipment sensor for long-term statistics#74
robbybarnes wants to merge 2 commits intojmdevita:mainfrom
robbybarnes:feature/add-state-class-to-sensors

Conversation

@robbybarnes
Copy link

Summary

Adds SensorStateClass.MEASUREMENT to the CollectionShipment sensor, enabling Home Assistant's long-term statistics tracking.

Motivation

Home Assistant can record long-term statistics for sensors that have a state_class defined. Without this, the collectable parcels count isn't available in the Statistics dashboard or for historical analysis.

This is useful for users who want to:

  • Track how many packages required pickup over time
  • Create graphs showing pickup frequency
  • Use statistics in automations or dashboards

Changes

  • Import SensorStateClass from homeassistant.components.sensor
  • Add _attr_state_class = SensorStateClass.MEASUREMENT to CollectionShipment
  • Add _attr_native_unit_of_measurement = "parcels" for proper unit display

Technical Notes

  • MEASUREMENT is the appropriate state class for a point-in-time count value
  • The unit of measurement helps Home Assistant display the value correctly in graphs
  • No changes to existing functionality; purely additive

Testing

  • All tests pass (11/11)
  • Verified sensor metadata includes new state_class

…ensor

Adds SensorStateClass.MEASUREMENT and native_unit_of_measurement to the
CollectionShipment sensor, enabling better integration with Home Assistant's
long-term statistics and energy dashboard features.

Changes:
- Import SensorStateClass from homeassistant.components.sensor
- Add _attr_state_class = SensorStateClass.MEASUREMENT
- Add _attr_native_unit_of_measurement = "parcels"

This allows the sensor to be properly tracked in HA's statistics and
provides better metadata for display and graphing.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@PineappleEmperor PineappleEmperor self-requested a review January 24, 2026 12:45
Copy link
Collaborator

@PineappleEmperor PineappleEmperor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one works. We may wish to modify the active sensor to always return an integer state and use the same setup. Would you like to incorporate those changes into this PR?

Per maintainer feedback, extends the state_class addition to ActiveShipment:
- Added SensorStateClass.MEASUREMENT and native_unit_of_measurement
- State now returns integer count of active parcels (enables HA statistics)
- Previous verbose text ("1 parcel", "in 2 days") moved to status_text attribute

This is a breaking change for users relying on the text state value,
but enables Home Assistant's long-term statistics tracking.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants