mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Improve code quality season (#66449)
This commit is contained in:
parent
152dbfd2fe
commit
759b01bb40
@ -1,13 +1,16 @@
|
|||||||
"""Support for tracking which astronomical or meteorological season it is."""
|
"""Support for tracking which astronomical or meteorological season it is."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import date, datetime
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import ephem
|
import ephem
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
||||||
|
SensorEntity,
|
||||||
|
)
|
||||||
from homeassistant.const import CONF_NAME, CONF_TYPE
|
from homeassistant.const import CONF_NAME, CONF_TYPE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -49,7 +52,7 @@ SEASON_ICONS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_TYPE, default=TYPE_ASTRONOMICAL): vol.In(VALID_TYPES),
|
vol.Optional(CONF_TYPE, default=TYPE_ASTRONOMICAL): vol.In(VALID_TYPES),
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
@ -64,8 +67,8 @@ def setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Display the current season."""
|
"""Display the current season."""
|
||||||
_type = config.get(CONF_TYPE)
|
_type: str = config[CONF_TYPE]
|
||||||
name = config.get(CONF_NAME)
|
name: str = config[CONF_NAME]
|
||||||
|
|
||||||
if hass.config.latitude < 0:
|
if hass.config.latitude < 0:
|
||||||
hemisphere = SOUTHERN
|
hemisphere = SOUTHERN
|
||||||
@ -75,33 +78,35 @@ def setup_platform(
|
|||||||
hemisphere = EQUATOR
|
hemisphere = EQUATOR
|
||||||
|
|
||||||
_LOGGER.debug(_type)
|
_LOGGER.debug(_type)
|
||||||
add_entities([Season(hass, hemisphere, _type, name)], True)
|
add_entities([Season(hemisphere, _type, name)], True)
|
||||||
|
|
||||||
|
|
||||||
def get_season(date, hemisphere, season_tracking_type):
|
def get_season(
|
||||||
|
current_date: date, hemisphere: str, season_tracking_type: str
|
||||||
|
) -> str | None:
|
||||||
"""Calculate the current season."""
|
"""Calculate the current season."""
|
||||||
|
|
||||||
if hemisphere == "equator":
|
if hemisphere == "equator":
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if season_tracking_type == TYPE_ASTRONOMICAL:
|
if season_tracking_type == TYPE_ASTRONOMICAL:
|
||||||
spring_start = ephem.next_equinox(str(date.year)).datetime()
|
spring_start = ephem.next_equinox(str(current_date.year)).datetime()
|
||||||
summer_start = ephem.next_solstice(str(date.year)).datetime()
|
summer_start = ephem.next_solstice(str(current_date.year)).datetime()
|
||||||
autumn_start = ephem.next_equinox(spring_start).datetime()
|
autumn_start = ephem.next_equinox(spring_start).datetime()
|
||||||
winter_start = ephem.next_solstice(summer_start).datetime()
|
winter_start = ephem.next_solstice(summer_start).datetime()
|
||||||
else:
|
else:
|
||||||
spring_start = datetime(2017, 3, 1).replace(year=date.year)
|
spring_start = datetime(2017, 3, 1).replace(year=current_date.year)
|
||||||
summer_start = spring_start.replace(month=6)
|
summer_start = spring_start.replace(month=6)
|
||||||
autumn_start = spring_start.replace(month=9)
|
autumn_start = spring_start.replace(month=9)
|
||||||
winter_start = spring_start.replace(month=12)
|
winter_start = spring_start.replace(month=12)
|
||||||
|
|
||||||
if spring_start <= date < summer_start:
|
if spring_start <= current_date < summer_start:
|
||||||
season = STATE_SPRING
|
season = STATE_SPRING
|
||||||
elif summer_start <= date < autumn_start:
|
elif summer_start <= current_date < autumn_start:
|
||||||
season = STATE_SUMMER
|
season = STATE_SUMMER
|
||||||
elif autumn_start <= date < winter_start:
|
elif autumn_start <= current_date < winter_start:
|
||||||
season = STATE_AUTUMN
|
season = STATE_AUTUMN
|
||||||
elif winter_start <= date or spring_start > date:
|
elif winter_start <= current_date or spring_start > current_date:
|
||||||
season = STATE_WINTER
|
season = STATE_WINTER
|
||||||
|
|
||||||
# If user is located in the southern hemisphere swap the season
|
# If user is located in the southern hemisphere swap the season
|
||||||
@ -113,36 +118,20 @@ def get_season(date, hemisphere, season_tracking_type):
|
|||||||
class Season(SensorEntity):
|
class Season(SensorEntity):
|
||||||
"""Representation of the current season."""
|
"""Representation of the current season."""
|
||||||
|
|
||||||
def __init__(self, hass, hemisphere, season_tracking_type, name):
|
_attr_device_class = "season__season"
|
||||||
|
|
||||||
|
def __init__(self, hemisphere: str, season_tracking_type: str, name: str) -> None:
|
||||||
"""Initialize the season."""
|
"""Initialize the season."""
|
||||||
self.hass = hass
|
self._attr_name = name
|
||||||
self._name = name
|
|
||||||
self.hemisphere = hemisphere
|
self.hemisphere = hemisphere
|
||||||
self.datetime = None
|
|
||||||
self.type = season_tracking_type
|
self.type = season_tracking_type
|
||||||
self.season = None
|
|
||||||
|
|
||||||
@property
|
def update(self) -> None:
|
||||||
def name(self):
|
|
||||||
"""Return the name."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the current season."""
|
|
||||||
return self.season
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class."""
|
|
||||||
return "season__season"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Icon to use in the frontend, if any."""
|
|
||||||
return SEASON_ICONS.get(self.season, "mdi:cloud")
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update season."""
|
"""Update season."""
|
||||||
self.datetime = utcnow().replace(tzinfo=None)
|
self._attr_native_value = get_season(
|
||||||
self.season = get_season(self.datetime, self.hemisphere, self.type)
|
utcnow().replace(tzinfo=None), self.hemisphere, self.type
|
||||||
|
)
|
||||||
|
|
||||||
|
self._attr_icon = "mdi:cloud"
|
||||||
|
if self._attr_native_value:
|
||||||
|
self._attr_icon = SEASON_ICONS[self._attr_native_value]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user