Skip to content

Commit

Permalink
fix: multiple sensors #5
Browse files Browse the repository at this point in the history
  • Loading branch information
chilikla authored Oct 22, 2024
1 parent 4b4d604 commit 20d933f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
4 changes: 2 additions & 2 deletions custom_components/yerushamayim/data_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _extract_data(self) -> YerushamayimData:
forecast_text_child = forecast_line.select(".forcast_text .likedislike")[0].get_text()
forecast_text = forecast_text.replace(forecast_text_child, "").strip().replace("\n", "")
day_icon = forecast_line.select(".icon_day img")[0]["src"]
condition = day_icon.replace("https://www.02ws.co.il/images/icons/day/n4_", "").replace(".svg", "")
condition = day_icon.replace("images/icons/day/n4_", "").replace(".svg", "")
status_data = {"forecast": forecast_text, "day_icon": URL + day_icon, "condition": condition}

if self.coldmeter_api is not None and self.coldmeter_api.data:
Expand All @@ -143,7 +143,7 @@ def _extract_data(self) -> YerushamayimData:
status_data.update({
"status": coldmeter["coldmeter"]["current_feeling"],
"cloth_icon": URL + "images/clothes/" + coldmeter["coldmeter"]["cloth_name"],
"cloth_text": coldmeter["coldmeter"]["clothtitle"],
"cloth_info": coldmeter["coldmeter"]["clothtitle"],
"laundry": coldmeter.get("laundryidx", {}).get("laundry_con_title", None)
})
except Exception as err:
Expand Down
57 changes: 50 additions & 7 deletions custom_components/yerushamayim/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Sensor platform for Yerushamayim integration."""
from __future__ import annotations
from datetime import datetime

from homeassistant.components.sensor import (
SensorEntity,
Expand Down Expand Up @@ -51,11 +52,6 @@ def sensor_type(self) -> str:
"""Return the sensor type."""
raise NotImplementedError

@property
def native_value(self) -> None:
"""No native value for these sensors."""
return None

@property
def extra_state_attributes(self):
"""Return the state attributes."""
Expand All @@ -81,6 +77,20 @@ def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return UnitOfTemperature.CELSIUS

@property
def native_value(self):
"""Return the temperature value."""
temp = self.coordinator.data.temperature.get("temperature")
try:
return float(temp) if temp is not None else None
except (ValueError, TypeError):
return None

@property
def icon(self):
"""Return the icon."""
return "mdi:thermometer"

@property
def extra_state_attributes(self):
"""Return the state attributes with numeric conversions."""
Expand Down Expand Up @@ -114,6 +124,20 @@ def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return PERCENTAGE

@property
def native_value(self):
"""Return the humidity value."""
humidity = self.coordinator.data.humidity.get("humidity")
try:
return float(humidity) if humidity is not None else None
except (ValueError, TypeError):
return None

@property
def icon(self):
"""Return the icon."""
return "mdi:water-percent"

@property
def extra_state_attributes(self):
"""Return the state attributes with numeric conversion."""
Expand All @@ -131,19 +155,38 @@ class YerushamayimStatusSensor(YerushamayimBaseSensor):

sensor_type = "status"

@property
def native_value(self):
"""Return the forecast text."""
return self.coordinator.data.status.get("forecast")

@property
def icon(self):
"""Return the icon of the sensor."""
"""Return the icon."""
return "mdi:weather-sunny"

class YerushamayimForecastSensor(YerushamayimBaseSensor):
"""Forecast sensor for Yerushamayim."""

sensor_type = "forecast"

@property
def native_value(self):
"""Return the day of the week."""
days = {
0: "Monday",
1: "Tuesday",
2: "Wednesday",
3: "Thursday",
4: "Friday",
5: "Saturday",
6: "Sunday"
}
return days[datetime.now().weekday()]

@property
def icon(self):
"""Return the icon of the sensor."""
"""Return the icon."""
return "mdi:clock-outline"

@property
Expand Down

0 comments on commit 20d933f

Please sign in to comment.