Skip to content

Commit

Permalink
Add device info in sensor and weather entities
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunglung committed Feb 21, 2024
1 parent 9e1dcb6 commit 775a49e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
13 changes: 12 additions & 1 deletion custom_components/aoaws_anws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""The ANWS AOAWS integration."""
"""The Taiwan ANWS integration."""
import asyncio
import logging

Expand All @@ -7,6 +7,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo

from .const import (
CONF_LANGUAGE,
Expand Down Expand Up @@ -95,3 +96,13 @@ def _get_config_value(config_entry, key, default):
if config_entry.options:
return config_entry.options.get(key, default)
return config_entry.data.get(key, default)

def device_info(config_entry: ConfigEntry) -> DeviceInfo:
"""Build and return the device info for EC."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, config_entry.entry_id)},
manufacturer="Taiwan ANWS",
name=config_entry.title,
configuration_url="https://aoaws.anws.gov.tw/AWS",
)
15 changes: 15 additions & 0 deletions custom_components/aoaws_anws/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
PERCENTAGE,
UnitOfSpeed,
UnitOfTemperature,
UnitOfPressure,
UV_INDEX,
)
from homeassistant.components.sensor import SensorDeviceClass
Expand Down Expand Up @@ -211,6 +212,20 @@
"mdi:eye",
True,
],
"pressure": [
"Air Pressure",
SensorDeviceClass.ATMOSPHERIC_PRESSURE,
UnitOfPressure.HPA,
"mdi:gauge",
True
],
"dew_point": [
"Dew Point",
SensorDeviceClass.TEMPERATURE,
UnitOfTemperature.CELSIUS,
None,
True
],
# "uv": ["UV Index", None, UV_INDEX, "mdi:weather-sunny-alert", False],
# "precipitation": [
# "Probability of Precipitation",
Expand Down
10 changes: 9 additions & 1 deletion custom_components/aoaws_anws/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def __init__(self):
self.pressure = None
self.pressure_tendency = None
self.dew_point = None
self.cloud_coverage = None
self.cloud_ceiling = None

def __iter__(self):
for attr, value in self.__dict__.items():
Expand Down Expand Up @@ -116,6 +118,7 @@ def _convert_to_observation(self, site, data):
value = int(''.join(c for c in i[19] if c.isdigit()))
#unit = ''.join(c for c in i[19] if not c.isdigit()).replace(" ", " ")
unit = UnitOfTemperature.CELSIUS
temperature = value
observation.temperature = Element("T", value=value, units=unit.strip())

# wind speed
Expand Down Expand Up @@ -159,11 +162,16 @@ def _convert_to_observation(self, site, data):
value = value + 10
observation.visibility = Element("W", value=value, units=unit.strip())
for k in metar:
if "/" in k:
if "/" in k and temperature == int(k.split("/")[0]):
observation.dew_point = Element("T", value=k.split("/")[1])
if len(k) >= 1 and "Q" == k[0]:
observation.pressure = Element("P", value=k[1:])

# cloud ceiling
value = ''.join(c for c in i[18].replace(" ", " ") if c.isalpha() or c.isspace()).strip()
unit = ''.join(c for c in i[18].replace(" ", " ") if not c.isdigit())
observation.cloud_ceiling = Element("W", value=value, units=unit.strip())

return observation
return None

Expand Down
2 changes: 1 addition & 1 deletion custom_components/aoaws_anws/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Taiwan Navigation Weather Services (NWS)",
"documentation": "https://github.com/tsunglung/TwANWS",
"issue_tracker": "https://github.com/tsunglung/TwANWS/issues",
"version": "1.0.0",
"version": "1.0.3",
"requirements": ["bs4"],
"codeowners": ["@tsunglung"],
"config_flow": true,
Expand Down
12 changes: 7 additions & 5 deletions custom_components/aoaws_anws/sensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Support for UK Met Office weather service."""
"""Support for Taiwan ANWS weather service."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.typing import ConfigType

from . import device_info
from .const import (
ATTRIBUTION,
ATTR_LAST_UPDATE,
Expand All @@ -23,29 +24,30 @@
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigType, async_add_entities
) -> None:
"""Set up the Met Office weather sensor platform."""
"""Set up the Taiwan ANWS weather sensor platform."""
hass_data = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
[
AnwsAoawsCurrentSensor(entry.data, hass_data, sensor_type)
AnwsAoawsCurrentSensor(entry, hass_data, sensor_type)
for sensor_type in SENSOR_TYPES
],
False,
)


class AnwsAoawsCurrentSensor(SensorEntity):
"""Implementation of a Met Office current weather condition sensor."""
"""Implementation of a Taiwan ANWS current weather condition sensor."""

def __init__(self, entry_data, hass_data, sensor_type):
def __init__(self, config_entry, hass_data, sensor_type):
"""Initialize the sensor."""
self._data = hass_data[ANWS_AOAWS_DATA]
self._coordinator = hass_data[ANWS_AOAWS_COORDINATOR]

self._type = sensor_type
self._name = f"{hass_data[ANWS_AOAWS_NAME]} {SENSOR_TYPES[self._type][0]}"
self._unique_id = f"{SENSOR_TYPES[self._type][0]}_{self._data.site_name}"
self._attr_device_info = device_info(config_entry)

self.anws_aoaws_site_id = None
self.anws_aoaws_site_name = None
Expand Down
31 changes: 21 additions & 10 deletions custom_components/aoaws_anws/weather.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Support for ANWS AOAWS service."""
from homeassistant.components.weather import WeatherEntity
"""Support for Taiwan ANWS service."""
from homeassistant.components.weather import WeatherEntityFeature, SingleCoordinatorWeatherEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.typing import ConfigType

from . import device_info
from .const import (
ATTRIBUTION,
CONDITION_CLASSES,
Expand All @@ -17,30 +18,32 @@
async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigType, async_add_entities
) -> None:
"""Set up the Anws Aoaws weather sensor platform."""
"""Set up the Taiwan ANWS weather sensor platform."""
hass_data = hass.data[DOMAIN][config_entry.entry_id]

async_add_entities(
[
AnwsAoawsWeather(
config_entry.data,
config_entry,
hass_data,
)
],
False,
)


class AnwsAoawsWeather(WeatherEntity):
class AnwsAoawsWeather(SingleCoordinatorWeatherEntity):
"""Implementation of a Anws Aoaws weather condition."""
_attr_supported_features = WeatherEntityFeature.FORECAST_HOURLY

def __init__(self, entry_data, hass_data):
def __init__(self, config_entry, hass_data):
"""Initialise the platform with a data instance."""
self._data = hass_data[ANWS_AOAWS_DATA]
self._coordinator = hass_data[ANWS_AOAWS_COORDINATOR]

self._name = f"{DEFAULT_NAME} {hass_data[ANWS_AOAWS_NAME]}"
self._unique_id = f"{self._data.site_name}"
self._attr_device_info = device_info(config_entry)

self.anws_aoaws_now = None

Expand All @@ -66,8 +69,8 @@ def condition(self):
def cloud_coverage(self) -> float | None:
"""Return the Cloud coverage in %."""
return (
self.anws_aoaws_now.visibility.value
if self.anws_aoaws_now and self.anws_aoaws_now.visibility
self.anws_aoaws_now.cloud_coverage.value
if self.anws_aoaws_now and self.anws_aoaws_now.cloud_coverage
else None
)

Expand Down Expand Up @@ -110,12 +113,20 @@ def humidity(self) -> float | None:
@property
def native_dew_point(self) -> float | None:
"""Return the dew point."""
return None
return (
self.anws_aoaws_now.dew_point.value
if self.anws_aoaws_now and self.anws_aoaws_now.dew_point
else None
)

@property
def native_wind_gust_speed(self) -> float | None:
"""Return the wind gust speed."""
return None
return (
self.anws_aoaws_now.wind_speed.value
if self.anws_aoaws_now and self.anws_aoaws_now.wind_speed
else None
)

@property
def native_wind_speed(self) -> float | None:
Expand Down

0 comments on commit 775a49e

Please sign in to comment.