Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Season sensors."""
2 
3 from __future__ import annotations
4 
5 from datetime import date, datetime
6 
7 import ephem
8 
9 from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_TYPE
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.util.dt import utcnow
16 
17 from .const import DOMAIN, TYPE_ASTRONOMICAL
18 
19 EQUATOR = "equator"
20 
21 NORTHERN = "northern"
22 
23 SOUTHERN = "southern"
24 STATE_AUTUMN = "autumn"
25 STATE_SPRING = "spring"
26 STATE_SUMMER = "summer"
27 STATE_WINTER = "winter"
28 
29 HEMISPHERE_SEASON_SWAP = {
30  STATE_WINTER: STATE_SUMMER,
31  STATE_SPRING: STATE_AUTUMN,
32  STATE_AUTUMN: STATE_SPRING,
33  STATE_SUMMER: STATE_WINTER,
34 }
35 
36 
38  hass: HomeAssistant,
39  entry: ConfigEntry,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up the platform from config entry."""
43  hemisphere = EQUATOR
44  if hass.config.latitude < 0:
45  hemisphere = SOUTHERN
46  elif hass.config.latitude > 0:
47  hemisphere = NORTHERN
48 
49  async_add_entities([SeasonSensorEntity(entry, hemisphere)], True)
50 
51 
53  current_date: date, hemisphere: str, season_tracking_type: str
54 ) -> str | None:
55  """Calculate the current season."""
56 
57  if hemisphere == "equator":
58  return None
59 
60  if season_tracking_type == TYPE_ASTRONOMICAL:
61  spring_start = ephem.next_equinox(str(current_date.year)).datetime()
62  summer_start = ephem.next_solstice(str(current_date.year)).datetime()
63  autumn_start = ephem.next_equinox(spring_start).datetime()
64  winter_start = ephem.next_solstice(summer_start).datetime()
65  else:
66  spring_start = datetime(2017, 3, 1).replace(year=current_date.year)
67  summer_start = spring_start.replace(month=6)
68  autumn_start = spring_start.replace(month=9)
69  winter_start = spring_start.replace(month=12)
70 
71  season = STATE_WINTER
72  if spring_start <= current_date < summer_start:
73  season = STATE_SPRING
74  elif summer_start <= current_date < autumn_start:
75  season = STATE_SUMMER
76  elif autumn_start <= current_date < winter_start:
77  season = STATE_AUTUMN
78 
79  # If user is located in the southern hemisphere swap the season
80  if hemisphere == NORTHERN:
81  return season
82  return HEMISPHERE_SEASON_SWAP.get(season)
83 
84 
86  """Representation of the current season."""
87 
88  _attr_device_class = SensorDeviceClass.ENUM
89  _attr_has_entity_name = True
90  _attr_name = None
91  _attr_options = ["spring", "summer", "autumn", "winter"]
92  _attr_translation_key = "season"
93 
94  def __init__(self, entry: ConfigEntry, hemisphere: str) -> None:
95  """Initialize the season."""
96  self._attr_unique_id_attr_unique_id = entry.entry_id
97  self.hemispherehemisphere = hemisphere
98  self.typetype = entry.data[CONF_TYPE]
99  self._attr_device_info_attr_device_info = DeviceInfo(
100  name="Season",
101  identifiers={(DOMAIN, entry.entry_id)},
102  entry_type=DeviceEntryType.SERVICE,
103  )
104 
105  def update(self) -> None:
106  """Update season."""
107  self._attr_native_value_attr_native_value = get_season(
108  utcnow().replace(tzinfo=None), self.hemispherehemisphere, self.typetype
109  )
None __init__(self, ConfigEntry entry, str hemisphere)
Definition: sensor.py:94
str|None get_season(date current_date, str hemisphere, str season_tracking_type)
Definition: sensor.py:54
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:41