meteocat 0.1.22 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.1.23](https://github.com/figorr/meteocat/compare/v0.1.22...v0.1.23) (2024-12-07)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * 0.1.23 ([dda17ae](https://github.com/figorr/meteocat/commit/dda17ae1d73d31879d029d4c7a8f12a1a74f2379))
7
+ * fix sensor data recovery ([564ceb7](https://github.com/figorr/meteocat/commit/564ceb7ff372acd7d2d035272e9784ad583ccece))
8
+ * fix unis of measurement ([c65bce2](https://github.com/figorr/meteocat/commit/c65bce26add578bc32ebb05b82381aa72dfbf9a6))
9
+
1
10
  ## [0.1.22](https://github.com/figorr/meteocat/compare/v0.1.21...v0.1.22) (2024-12-07)
2
11
 
3
12
 
@@ -12,7 +12,7 @@ from .const import DOMAIN, PLATFORMS
12
12
  _LOGGER = logging.getLogger(__name__)
13
13
 
14
14
  # Versión
15
- __version__ = "0.1.22"
15
+ __version__ = "0.1.23"
16
16
 
17
17
 
18
18
  async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
@@ -10,25 +10,16 @@ STATION_NAME = "station_name"
10
10
  STATION_ID = "station_id"
11
11
 
12
12
  # Códigos de sensores de la API
13
- WIND_SPEED = "30" # Velocidad del viento
14
- WIND_DIRECTION = "31" # Dirección del viento
15
- TEMPERATURE = "32" # Temperatura
16
- HUMIDITY = "33" # Humedad relativa
17
- PRESSURE = "34" # Presión atmosférica
18
- PRECIPITATION = "35" # Precipitación
19
- UV_INDEX = "39" # UV
20
- MAX_TEMPERATURE = "40" # Temperatura máxima
21
- MIN_TEMPERATURE = "42" # Temperatura mínima
22
- WIND_GUST = "50" # Racha de viento
23
-
24
- # Unidades de medida de los sensores
25
- WIND_SPEED_UNIT = "m/s"
26
- WIND_DIRECTION_UNIT = "°"
27
- TEMPERATURE_UNIT = "°C"
28
- HUMIDITY_UNIT = "%"
29
- PRESSURE_UNIT = "hPa"
30
- PRECIPITATION_UNIT = "mm"
31
- UV_INDEX_UNIT = "UV"
13
+ WIND_SPEED = "wind_speed" # Velocidad del viento
14
+ WIND_DIRECTION = "wind_direction" # Dirección del viento
15
+ TEMPERATURE = "temperature" # Temperatura
16
+ HUMIDITY = "humidity" # Humedad relativa
17
+ PRESSURE = "pressure" # Presión atmosférica
18
+ PRECIPITATION = "precipitation" # Precipitación
19
+ UV_INDEX = "uv_index" # UV
20
+ MAX_TEMPERATURE = "max_temperature" # Temperatura máxima
21
+ MIN_TEMPERATURE = "min_temperature" # Temperatura mínima
22
+ WIND_GUST = "wind_gust" # Racha de viento
32
23
 
33
24
  # Mapeo de códigos 'estatCel' a condiciones de Home Assistant
34
25
  CONDITION_MAPPING = {
@@ -45,8 +36,19 @@ CONDITION_MAPPING = {
45
36
  "snow-rainy": [27, 29, 30],
46
37
  }
47
38
 
48
- # Platforms
49
- PLATFORMS = {
50
- "sensor",
51
- "entity",
39
+ # Mapeo de códigos a claves de sensores
40
+ VARIABLE_CODE_MAPPING = {
41
+ 30: WIND_SPEED,
42
+ 31: WIND_DIRECTION,
43
+ 32: TEMPERATURE,
44
+ 33: HUMIDITY,
45
+ 34: PRESSURE,
46
+ 35: PRECIPITATION,
47
+ 39: UV_INDEX,
48
+ 40: MAX_TEMPERATURE,
49
+ 42: MIN_TEMPERATURE,
50
+ 50: WIND_GUST,
52
51
  }
52
+
53
+ # Platforms
54
+ PLATFORMS = ["sensor"]
@@ -3,7 +3,14 @@ from __future__ import annotations
3
3
  import asyncio
4
4
  import logging
5
5
  from homeassistant.components.weather import WeatherEntity
6
- from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
6
+ from homeassistant.const import (
7
+ DEGREE,
8
+ PERCENTAGE,
9
+ UnitOfPressure,
10
+ UnitOfSpeed,
11
+ UnitOfTemperature,
12
+ UnitOfVolumetricFlux,
13
+ )
7
14
 
8
15
  from .const import (
9
16
  DOMAIN,
@@ -38,7 +45,7 @@ class MeteocatWeatherEntity(WeatherEntity):
38
45
  def __init__(self, coordinator: MeteocatEntityCoordinator):
39
46
  """Inicializa la entidad MeteocatWeather."""
40
47
  self._coordinator = coordinator
41
- self._attr_temperature_unit = TEMP_CELSIUS
48
+ self._attr_temperature_unit = UnitOfTemperature.CELSIUS
42
49
  self._data = {}
43
50
 
44
51
  async def async_update(self):
@@ -8,5 +8,5 @@
8
8
  "documentation": "https://gitlab.com/figorr/meteocat",
9
9
  "loggers": ["meteocatpy"],
10
10
  "requirements": ["meteocatpy==0.0.7", "packaging>=20.3", "wrapt>=1.14.0"],
11
- "version": "0.1.22"
11
+ "version": "0.1.23"
12
12
  }
@@ -11,7 +11,14 @@ from homeassistant.components.sensor import (
11
11
  from homeassistant.core import callback
12
12
  from homeassistant.helpers.entity_platform import AddEntitiesCallback
13
13
  from homeassistant.helpers.update_coordinator import CoordinatorEntity
14
- from homeassistant.const import PERCENTAGE, UnitOfTemperature
14
+ from homeassistant.const import (
15
+ DEGREE,
16
+ PERCENTAGE,
17
+ UnitOfPressure,
18
+ UnitOfSpeed,
19
+ UnitOfTemperature,
20
+ UnitOfVolumetricFlux,
21
+ )
15
22
 
16
23
  from .const import (
17
24
  DOMAIN,
@@ -25,11 +32,7 @@ from .const import (
25
32
  MAX_TEMPERATURE,
26
33
  MIN_TEMPERATURE,
27
34
  WIND_GUST,
28
- WIND_SPEED_UNIT,
29
- PRESSURE_UNIT,
30
- PRECIPITATION_UNIT,
31
- UV_INDEX_UNIT,
32
- WIND_DIRECTION_UNIT,
35
+ VARIABLE_CODE_MAPPING,
33
36
  )
34
37
 
35
38
  from .coordinator import MeteocatSensorCoordinator
@@ -47,14 +50,13 @@ SENSOR_TYPES: tuple[MeteocatSensorEntityDescription, ...] = (
47
50
  icon="mdi:weather-windy",
48
51
  device_class=SensorDeviceClass.WIND_SPEED,
49
52
  state_class=SensorStateClass.MEASUREMENT,
50
- native_unit_of_measurement=WIND_SPEED_UNIT,
53
+ native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
51
54
  ),
52
55
  MeteocatSensorEntityDescription(
53
56
  key=WIND_DIRECTION,
54
57
  name="Wind Direction",
55
58
  icon="mdi:compass",
56
59
  device_class=None,
57
- native_unit_of_measurement=WIND_DIRECTION_UNIT,
58
60
  ),
59
61
  MeteocatSensorEntityDescription(
60
62
  key=TEMPERATURE,
@@ -78,7 +80,7 @@ SENSOR_TYPES: tuple[MeteocatSensorEntityDescription, ...] = (
78
80
  icon="mdi:gauge",
79
81
  device_class=SensorDeviceClass.ATMOSPHERIC_PRESSURE,
80
82
  state_class=SensorStateClass.MEASUREMENT,
81
- native_unit_of_measurement=PRESSURE_UNIT,
83
+ native_unit_of_measurement=UnitOfPressure.HPA,
82
84
  ),
83
85
  MeteocatSensorEntityDescription(
84
86
  key=PRECIPITATION,
@@ -86,14 +88,12 @@ SENSOR_TYPES: tuple[MeteocatSensorEntityDescription, ...] = (
86
88
  icon="mdi:weather-rainy",
87
89
  device_class=None,
88
90
  state_class=SensorStateClass.MEASUREMENT,
89
- native_unit_of_measurement=PRECIPITATION_UNIT,
91
+ native_unit_of_measurement=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
90
92
  ),
91
93
  MeteocatSensorEntityDescription(
92
94
  key=UV_INDEX,
93
95
  name="UV Index",
94
96
  icon="mdi:weather-sunny",
95
- state_class=SensorStateClass.MEASUREMENT,
96
- native_unit_of_measurement=UV_INDEX_UNIT,
97
97
  ),
98
98
  MeteocatSensorEntityDescription(
99
99
  key=MAX_TEMPERATURE,
@@ -117,7 +117,7 @@ SENSOR_TYPES: tuple[MeteocatSensorEntityDescription, ...] = (
117
117
  icon="mdi:weather-windy",
118
118
  device_class=SensorDeviceClass.WIND_SPEED,
119
119
  state_class=SensorStateClass.MEASUREMENT,
120
- native_unit_of_measurement=WIND_SPEED_UNIT,
120
+ native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
121
121
  ),
122
122
  )
123
123
 
@@ -155,25 +155,40 @@ class MeteocatSensor(CoordinatorEntity[MeteocatSensorCoordinator], SensorEntity)
155
155
  @property
156
156
  def native_value(self):
157
157
  """Return the state of the sensor."""
158
- value = getattr(self.coordinator.data, self.entity_description.key, None)
158
+ sensor_code = next(
159
+ (code for code, key in VARIABLE_CODE_MAPPING.items() if key == self.entity_description.key),
160
+ None,
161
+ )
162
+
163
+ if sensor_code is not None:
164
+ variable_data = next(
165
+ (var for var in self.coordinator.data.get("variables", []) if var["codi"] == sensor_code),
166
+ None,
167
+ )
168
+ if variable_data:
169
+ # Asume que quieres el último valor registrado
170
+ latest_reading = variable_data["lectures"][-1]
171
+ value = latest_reading.get("valor")
159
172
 
160
- if self.entity_description.key == WIND_DIRECTION:
161
- return self._convert_degrees_to_cardinal(value)
173
+ # Convertir grados a direcciones cardinales para WIND_DIRECTION
174
+ if self.entity_description.key == WIND_DIRECTION and isinstance(value, (int, float)):
175
+ return self._convert_degrees_to_cardinal(value)
162
176
 
163
- return value
177
+ return value
178
+
179
+ return None
164
180
 
165
181
  @staticmethod
166
- def _convert_degrees_to_cardinal(degree: float | None) -> str | None:
182
+ def _convert_degrees_to_cardinal(degree: float) -> str:
167
183
  """Convert degrees to cardinal direction."""
168
- if degree is None:
169
- return None
170
184
  directions = [
171
185
  "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
172
186
  "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N",
173
187
  ]
174
- index = int(((degree + 11.25) / 22.5)) % 16
188
+ index = round(degree / 22.5) % 16
175
189
  return directions[index]
176
190
 
191
+
177
192
  @property
178
193
  def device_info(self) -> DeviceInfo:
179
194
  """Return the device info."""
@@ -1,2 +1,2 @@
1
1
  # version.py
2
- __version__ = "0.1.22"
2
+ __version__ = "0.1.23"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meteocat",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "description": "[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\r [![Python version compatibility](https://img.shields.io/pypi/pyversions/meteocat)](https://pypi.org/project/meteocat)\r [![pipeline status](https://gitlab.com/figorr/meteocat/badges/master/pipeline.svg)](https://gitlab.com/figorr/meteocat/commits/master)",
5
5
  "main": "index.js",
6
6
  "directories": {
package/pyproject.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "meteocat"
3
- version = "0.1.22"
3
+ version = "0.1.23"
4
4
  description = "Script para obtener datos meteorológicos de la API de Meteocat"
5
5
  authors = ["figorr <jdcuartero@yahoo.es>"]
6
6
  license = "Apache-2.0"