1 """Support for retrieving status info from Google Wifi/OnHub routers."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
6 from datetime
import timedelta
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
15 SensorEntityDescription,
19 CONF_MONITORED_CONDITIONS,
29 _LOGGER = logging.getLogger(__name__)
31 ATTR_CURRENT_VERSION =
"current_version"
32 ATTR_LAST_RESTART =
"last_restart"
33 ATTR_LOCAL_IP =
"local_ip"
34 ATTR_NEW_VERSION =
"new_version"
35 ATTR_STATUS =
"status"
36 ATTR_UPTIME =
"uptime"
38 DEFAULT_HOST =
"testwifi.here"
39 DEFAULT_NAME =
"google_wifi"
41 ENDPOINT =
"/api/v1/status"
46 @dataclass(frozen=True, kw_only=True)
48 """Describes GoogleWifi sensor entity."""
54 SENSOR_TYPES: tuple[GoogleWifiSensorEntityDescription, ...] = (
56 key=ATTR_CURRENT_VERSION,
57 primary_key=
"software",
58 sensor_key=
"softwareVersion",
59 icon=
"mdi:checkbox-marked-circle-outline",
63 primary_key=
"software",
64 sensor_key=
"updateNewVersion",
71 native_unit_of_measurement=UnitOfTime.DAYS,
75 key=ATTR_LAST_RESTART,
83 sensor_key=
"localIpAddress",
84 icon=
"mdi:access-point-network",
94 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
96 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
98 vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
99 vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
100 cv.ensure_list, [vol.In(SENSOR_KEYS)]
102 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
110 add_entities: AddEntitiesCallback,
111 discovery_info: DiscoveryInfoType |
None =
None,
113 """Set up the Google Wifi sensor."""
114 name = config[CONF_NAME]
115 host = config[CONF_HOST]
116 monitored_conditions = config[CONF_MONITORED_CONDITIONS]
121 for description
in SENSOR_TYPES
122 if description.key
in monitored_conditions
128 """Representation of a Google Wifi sensor."""
130 entity_description: GoogleWifiSensorEntityDescription
136 description: GoogleWifiSensorEntityDescription,
138 """Initialize a Google Wifi sensor."""
145 """Return availability of Google Wifi API."""
146 return self.
_api_api.available
149 """Get the latest data from the Google Wifi API."""
158 """Get the latest data and update the states."""
161 """Initialize the data object."""
163 resource = f
"{uri}{host}{ENDPOINT}"
164 self.
_request_request = requests.Request(
"GET", resource).prepare()
168 ATTR_CURRENT_VERSION:
None,
169 ATTR_NEW_VERSION:
None,
171 ATTR_LAST_RESTART:
None,
178 @Throttle(MIN_TIME_BETWEEN_UPDATES)
180 """Get the latest data from the router."""
182 with requests.Session()
as sess:
183 response = sess.send(self.
_request_request, timeout=10)
184 self.
raw_dataraw_data = response.json()
187 except (ValueError, requests.exceptions.ConnectionError):
188 _LOGGER.warning(
"Unable to fetch data from Google Wifi")
193 """Format raw data into easily accessible dict."""
194 for description
in SENSOR_TYPES:
195 if description.key
not in self.
conditionsconditions:
197 attr_key = description.key
199 if description.primary_key
in self.
raw_dataraw_data:
200 sensor_value = self.
raw_dataraw_data[description.primary_key][
201 description.sensor_key
204 if attr_key == ATTR_NEW_VERSION
and sensor_value ==
"0.0.0.0":
205 sensor_value =
"Latest"
206 elif attr_key == ATTR_UPTIME:
207 sensor_value = round(sensor_value / (3600 * 24), 2)
208 elif attr_key == ATTR_LAST_RESTART:
209 last_restart = dt_util.now() -
timedelta(seconds=sensor_value)
210 sensor_value = last_restart.strftime(
"%Y-%m-%d %H:%M:%S")
211 elif attr_key == ATTR_STATUS:
213 sensor_value =
"Online"
215 sensor_value =
"Offline"
217 attr_key == ATTR_LOCAL_IP
and not self.
raw_dataraw_data[
"wan"][
"online"]
221 self.
datadata[attr_key] = sensor_value
225 "Router does not support %s field. "
226 "Please remove %s from monitored_conditions"
228 description.sensor_key,
231 self.
datadata[attr_key] =
None
def __init__(self, host, conditions)
None __init__(self, GoogleWifiAPI api, str name, GoogleWifiSensorEntityDescription description)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)