Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Google Mail Sensors."""
2 
3 from __future__ import annotations
4 
5 from datetime import UTC, datetime, timedelta
6 
7 from googleapiclient.http import HttpRequest
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13 )
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import GoogleMailConfigEntry
18 from .entity import GoogleMailEntity
19 
20 SCAN_INTERVAL = timedelta(minutes=15)
21 
23  key="vacation_end_date",
24  translation_key="vacation_end_date",
25  device_class=SensorDeviceClass.TIMESTAMP,
26 )
27 
28 
30  hass: HomeAssistant,
31  entry: GoogleMailConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up the Google Mail sensor."""
35  async_add_entities([GoogleMailSensor(entry.runtime_data, SENSOR_TYPE)], True)
36 
37 
39  """Representation of a Google Mail sensor."""
40 
41  async def async_update(self) -> None:
42  """Get the vacation data."""
43  service = await self.authauth.get_resource()
44  settings: HttpRequest = service.users().settings().getVacation(userId="me")
45  data: dict = await self.hasshass.async_add_executor_job(settings.execute)
46 
47  if data["enableAutoReply"] and (end := data.get("endTime")):
48  value = datetime.fromtimestamp(int(end) / 1000, tz=UTC)
49  else:
50  value = None
51  self._attr_native_value_attr_native_value = value
None async_setup_entry(HomeAssistant hass, GoogleMailConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:33
str get_resource(str domain_name, ConfigType domain_data)
Definition: helpers.py:83