1 """The FireServiceRota integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from pyfireservicerota
import (
11 FireServiceRotaIncidents,
23 from .const
import DATA_CLIENT, DATA_COORDINATOR, DOMAIN, WSS_BWRURL
27 _LOGGER = logging.getLogger(__name__)
29 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
33 """Set up FireServiceRota from a config entry."""
35 hass.data.setdefault(DOMAIN, {})
40 if client.token_refresh_failure:
43 async
def async_update_data():
44 return await client.async_update()
50 name=
"duty binary sensor",
51 update_method=async_update_data,
52 update_interval=MIN_TIME_BETWEEN_UPDATES,
55 await coordinator.async_config_entry_first_refresh()
57 hass.data[DOMAIN][entry.entry_id] = {
59 DATA_COORDINATOR: coordinator,
62 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
68 """Unload FireServiceRota config entry."""
70 await hass.async_add_executor_job(
71 hass.data[DOMAIN][entry.entry_id].websocket.stop_listener
73 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
75 del hass.data[DOMAIN][entry.entry_id]
80 """Handle authentication tokens."""
83 """Initialize the oauth object."""
87 self.
_url_url = entry.data[CONF_URL]
92 """Refresh tokens and update config entry."""
93 _LOGGER.debug(
"Refreshing authentication tokens after expiration")
96 token_info = await self.
_hass_hass.async_add_executor_job(
97 self.
_fsr_fsr.refresh_tokens
100 except (InvalidAuthError, InvalidTokenError)
as err:
102 "Error refreshing tokens, triggered reauth workflow"
105 _LOGGER.debug(
"Saving new tokens in config entry")
106 self.
_hass_hass.config_entries.async_update_entry(
109 "auth_implementation": DOMAIN,
110 CONF_URL: self.
_url_url,
112 CONF_TOKEN: token_info,
120 """Define a FireServiceRota websocket manager object."""
123 """Initialize the websocket object."""
131 """Return URL with latest access token."""
132 return WSS_BWRURL.format(
133 self.
_entry_entry.data[CONF_URL], self.
_entry_entry.data[CONF_TOKEN][
"access_token"]
137 """Received new incident, update data."""
138 _LOGGER.debug(
"Received new incident via websocket: %s", data)
143 """Start the websocket listener."""
144 _LOGGER.debug(
"Starting incidents listener")
148 """Stop the websocket listener."""
149 _LOGGER.debug(
"Stopping incidents listener")
154 """Getting the latest data from fireservicerota."""
157 """Initialize the data object."""
161 self.
_url_url = entry.data[CONF_URL]
171 self.
fsrfsr = FireServiceRota(base_url=self.
_url_url, token_info=self.
_tokens_tokens)
182 """Set up the data client."""
183 await self.
_hass_hass.async_add_executor_job(self.
websocketwebsocket.start_listener)
186 """Perform update call and return data."""
191 return await self.
_hass_hass.async_add_executor_job(func, *args)
192 except (ExpiredTokenError, InvalidTokenError):
193 await self.
_hass_hass.async_add_executor_job(self.
websocketwebsocket.stop_listener)
196 if await self.
oauthoauth.async_refresh_tokens():
198 await self.
_hass_hass.async_add_executor_job(self.
websocketwebsocket.start_listener)
200 return await self.
_hass_hass.async_add_executor_job(func, *args)
203 """Get the latest availability data."""
205 self.
fsrfsr.get_availability,
str(self.
_hass_hass.config.time_zone)
211 self.
on_dutyon_duty = bool(data.get(
"available"))
213 _LOGGER.debug(
"Updated availability data: %s", data)
217 """Get the latest incident response data."""
222 _LOGGER.debug(
"Updating response data for incident id %s", self.
incident_idincident_id)
227 """Set incident response status."""
233 "Setting incident response for incident id '%s' to state '%s'",
def __init__(self, hass, entry)
None async_set_response(self, value)
dict|None async_response_update(self)
def update_call(self, func, *args)
dict|None async_update(self)
def __init__(self, hass, entry, fsr)
bool async_refresh_tokens(self)
None start_listener(self)
None _on_incident(self, data)
def __init__(self, hass, entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None dispatcher_send(HomeAssistant hass, str signal, *Any args)