1 """Support for Washington State Department of Transportation (WSDOT) data."""
3 from __future__
import annotations
5 from datetime
import datetime, timedelta, timezone
6 from http
import HTTPStatus
12 import voluptuous
as vol
15 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
24 _LOGGER = logging.getLogger(__name__)
26 ATTR_ACCESS_CODE =
"AccessCode"
27 ATTR_AVG_TIME =
"AverageTime"
28 ATTR_CURRENT_TIME =
"CurrentTime"
29 ATTR_DESCRIPTION =
"Description"
30 ATTR_TIME_UPDATED =
"TimeUpdated"
31 ATTR_TRAVEL_TIME_ID =
"TravelTimeID"
33 ATTRIBUTION =
"Data provided by WSDOT"
35 CONF_TRAVEL_TIMES =
"travel_time"
40 "http://www.wsdot.wa.gov/Traffic/api/TravelTimes/"
41 "TravelTimesREST.svc/GetTravelTimeAsJson"
46 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
48 vol.Required(CONF_API_KEY): cv.string,
49 vol.Required(CONF_TRAVEL_TIMES): [
50 {vol.Required(CONF_ID): cv.string, vol.Optional(CONF_NAME): cv.string}
59 add_entities: AddEntitiesCallback,
60 discovery_info: DiscoveryInfoType |
None =
None,
62 """Set up the WSDOT sensor."""
64 for travel_time
in config[CONF_TRAVEL_TIMES]:
65 name = travel_time.get(CONF_NAME)
or travel_time.get(CONF_ID)
68 name, config.get(CONF_API_KEY), travel_time.get(CONF_ID)
76 """Sensor that reads the WSDOT web API.
78 WSDOT provides ferry schedules, toll rates, weather conditions,
79 mountain pass conditions, and more. Subclasses of this
80 can read them and make them available.
86 """Initialize the sensor."""
94 """Return the name of the sensor."""
95 return self.
_name_name
99 """Return the state of the sensor."""
104 """Travel time sensor from WSDOT."""
106 _attr_attribution = ATTRIBUTION
107 _attr_native_unit_of_measurement = UnitOfTime.MINUTES
109 def __init__(self, name, access_code, travel_time_id):
110 """Construct a travel time sensor."""
112 WashingtonStateTransportSensor.__init__(self, name, access_code)
115 """Get the latest data from WSDOT."""
121 response = requests.get(RESOURCE, params, timeout=10)
122 if response.status_code != HTTPStatus.OK:
123 _LOGGER.warning(
"Invalid response from WSDOT API")
130 """Return other details about the sensor state."""
148 """Convert WSDOT timestamp to datetime."""
152 milliseconds, tzone = re.search(
r"Date\((\d+)([+-]\d\d)\d\d\)", timestamp).groups()
153 return datetime.fromtimestamp(
def __init__(self, name, access_code)
dict[str, Any]|None extra_state_attributes(self)
def __init__(self, name, access_code, travel_time_id)
web.Response get(self, web.Request request, str config_key)
def add_entities(account, async_add_entities, tracked)
def _parse_wsdot_timestamp(timestamp)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)