Skip to content

Commit

Permalink
Merge pull request #417 from maxyvon/outside-temp
Browse files Browse the repository at this point in the history
Modif sur la météo extérieur
  • Loading branch information
ic-dev21 authored Apr 17, 2024
2 parents 05a932a + 3a88447 commit efe55c2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
16 changes: 16 additions & 0 deletions custom_components/hilo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@

TARIFF_LIST = ["high", "medium", "low"]

WEATHER_CONDITIONS = {
"Unknown": "mdi:weather-sunny-alert",
"Blowing Snow": "mdi:weather-snowy-heavy",
"Clear": "mdi:weather-sunny",
"Cloudy": "mdi:weather-cloudy",
"Fair": "mdi:weather-partly-cloudy",
"Foggy": "mdi:weather-fog",
"Hail Sleet": "mdi:weather-hail",
"Mostly Cloudy": "mdi:weather-partly-cloudy",
"Rain": "mdi:weather-rainy",
"Rain Snow": "mdi:weather-snowy-rainy",
"Snow": "mdi:weather-snowy",
"Thunder": "mdi:weather-lightning",
"Windy": "mdi:weather-windy",
}

# Class lists
LIGHT_CLASSES = ["LightDimmer", "WhiteBulb", "ColorBulb", "LightSwitch"]
HILO_SENSOR_CLASSES = [
Expand Down
81 changes: 35 additions & 46 deletions custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CURRENCY_DOLLAR,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
STATE_UNKNOWN,
Platform,
UnitOfEnergy,
UnitOfPower,
Expand Down Expand Up @@ -56,6 +57,7 @@
NOTIFICATION_SCAN_INTERVAL,
REWARD_SCAN_INTERVAL,
TARIFF_LIST,
WEATHER_CONDITIONS,
)
from .managers import EnergyManager, UtilityManager

Expand Down Expand Up @@ -100,7 +102,7 @@ def generate_entities_from_device(device, hilo, scan_interval):
HiloNotificationSensor(hilo, device, scan_interval),
)
entities.append(
HiloOutDoorTempSensor(hilo, device, scan_interval),
HiloOutdoorTempSensor(hilo, device, scan_interval),
)
if device.has_attribute("battery"):
entities.append(BatterySensor(hilo, device))
Expand Down Expand Up @@ -840,7 +842,10 @@ async def async_update(self):
return


class HiloOutDoorTempSensor(HiloEntity, RestoreEntity, SensorEntity):
# class HiloOutdoorTempSensor(HiloEntity, RestoreEntity, SensorEntity):
# je ne crois pas qu'on a besoin d'un restoreentity pour une température.
# La dernière valeur n'a pas vraiment d'importance?
class HiloOutdoorTempSensor(HiloEntity, SensorEntity):
"""Hilo outdoor temperature sensor.
Its state will be the current outdoor weather as reported by the Hilo App
"""
Expand All @@ -852,16 +857,18 @@ class HiloOutDoorTempSensor(HiloEntity, RestoreEntity, SensorEntity):
def __init__(self, hilo, device, scan_interval):
self._attr_name = "Outdoor Weather Hilo"
super().__init__(hilo, name=self._attr_name, device=device)
old_unique_id = slugify(self._attr_name)
# old_unique_id = slugify(self._attr_name)
# pas requis pusisqu'on l'a jamais créé avec un autre uniqueID
# par contre on peut laisser pour être comme les autres sensors
self._attr_unique_id = (
f"{slugify(device.identifier)}-{slugify(self._attr_name)}"
)
hilo.async_migrate_unique_id(
old_unique_id, self._attr_unique_id, Platform.SENSOR
)
LOG.debug(f"Setting up OutDoorWeatherSensor entity: {self._attr_name}")
# hilo.async_migrate_unique_id(
# old_unique_id, self._attr_unique_id, Platform.SENSOR
# )
LOG.debug(f"Setting up OutdoorWeatherSensor entity: {self._attr_name}")
self.scan_interval = timedelta(seconds=EVENT_SCAN_INTERVAL_REDUCTION)
self._state = 0
self._state = STATE_UNKNOWN
self._weather = {}
self.async_update = Throttle(self.scan_interval)(self._async_update)

Expand All @@ -870,58 +877,40 @@ def state(self):
try:
return int(self._state)
except ValueError:
return 0
return STATE_UNKNOWN

@property
def icon(self):
condition = self._weather.get("condition", "").lower()
LOG.warning(f"Current condition: {condition}")
if not condition:
return "mdi:lan-disconnect"
if condition == "blowing snow":
return "mdi:weather-snowy"
elif condition == "clear":
return "mdi:weather-sunny"
elif condition == "cloudy":
return "mdi:weather-cloudy"
elif condition == "fair":
return "mdi:weather-partly-cloudy"
elif condition == "foggy":
return "mdi:weather-fog"
elif condition == "hail sleet":
return "mdi:weather-hail"
elif condition == "mostly cloudy":
return "mdi:weather-partly-cloudy"
elif condition == "rain":
return "mdi:weather-rainy"
elif condition == "rain snow":
return "mdi:weather-snowy-rainy"
elif condition == "snow":
return "mdi:weather-snowy"
elif condition == "thunder":
return "mdi:weather-lightning-rainy"
elif condition == "windy":
return "mdi:weather-windy"
elif condition == "unknown":
return "mdi:help-circle-outline"
else:
return "mdi:lan-disconnect"
# le code est moins lourd en utilisant une constate, en plus on garde une constante similaire à Hilo
return WEATHER_CONDITIONS.get(self._weather.get("condition", "Unknown"))

@property
def should_poll(self):
return True

@property
def extra_state_attributes(self):
return {"weather": self._weather}

async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
self._last_update = dt_util.utcnow()
self._state = last_state.state
LOG.debug(f"Adding weather {self._weather}")
# Les attributes n'avait pas l'aire créé séparément mais plutot juste en une seule string
# J'ai enlevé temperature puis qu'elle était difini 2 fois dans le fond.
# J'ai enlevé icon puisque c'était 0 et je voulais pas que ça rentre en conflis avec celle de HA
return {
key: self._weather[key]
for key in self._weather
if key not in ["temperature", "icon"]
}

# async def async_added_to_hass(self):
# """Handle entity about to be added to hass event."""
# await super().async_added_to_hass()
# last_state = await self.async_get_last_state()
# if last_state:
# self._last_update = dt_util.utcnow()
# self._state = last_state.state

async def _async_update(self):
self._weather = {}
Expand Down

0 comments on commit efe55c2

Please sign in to comment.