Transport NSW: Set DeviceClass and StateClass (#96928)

* 2023.7.16
- Fix bug with values defaulting to "n/a" in stead of None

* 2023.7.16
- Set device class and state classes on entities

* 2023.7.16
- Set StateClass and DeviceClass directly on the entitiy

* 2023.7.16
- Fix black and ruff issues

* 2023.7.17
- Update logic catering for the 'n/a' response on an API failure
- Add testcase

* - Fix bug in formatting

* 2023.7.17
- Refacotr to consider the "n/a" response returned from the Python lib
on an error or faliure
- Remove setting of StateClass and DeviceClass as requested
- Add "n/a" test case

* 2023.7.17
- Remove unused imports

* 2023.7.18
- Apply review requested changes

* - Additional review change resolved

* Add State and Device class attributes
This commit is contained in:
Tim 2023-07-20 16:35:26 +10:00 committed by GitHub
parent 23810752ed
commit 9da155955a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -8,7 +8,9 @@ import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import ATTR_MODE, CONF_API_KEY, CONF_NAME, UnitOfTime
from homeassistant.core import HomeAssistant
@ -73,6 +75,8 @@ class TransportNSWSensor(SensorEntity):
"""Implementation of an Transport NSW sensor."""
_attr_attribution = "Data provided by Transport NSW"
_attr_device_class = SensorDeviceClass.DURATION
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(self, data, stop_id, name):
"""Initialize the sensor."""

View file

@ -1,6 +1,10 @@
"""The tests for the Transport NSW (AU) sensor platform."""
from unittest.mock import patch
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -42,6 +46,8 @@ async def test_transportnsw_config(mocked_get_departures, hass: HomeAssistant) -
assert state.attributes["real_time"] == "y"
assert state.attributes["destination"] == "Palm Beach"
assert state.attributes["mode"] == "Bus"
assert state.attributes["device_class"] == SensorDeviceClass.DURATION
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT
def get_departuresMock_notFound(_stop_id, route, destination, api_key):