From 0a4f93a0cc4b6f7c34555f119ddc5105ae88f2ca Mon Sep 17 00:00:00 2001 From: tsunglung Date: Sat, 14 Dec 2024 14:20:21 +0800 Subject: [PATCH] Change the name and the unique_id of the entities --- custom_components/opencwb/__init__.py | 1 + custom_components/opencwb/const.py | 2 +- custom_components/opencwb/core/utils/opendata_cwb.py | 10 +++++----- custom_components/opencwb/sensor.py | 9 +++++---- custom_components/opencwb/weather.py | 4 +++- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/custom_components/opencwb/__init__.py b/custom_components/opencwb/__init__.py index 7861b05..89ae9d2 100755 --- a/custom_components/opencwb/__init__.py +++ b/custom_components/opencwb/__init__.py @@ -64,6 +64,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): hass.data[DOMAIN][config_entry.entry_id] = { ENTRY_NAME: name, ENTRY_WEATHER_COORDINATOR: weather_coordinator, + CONF_LOCATION_NAME: location_name } await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) diff --git a/custom_components/opencwb/const.py b/custom_components/opencwb/const.py index 130a678..be9f241 100755 --- a/custom_components/opencwb/const.py +++ b/custom_components/opencwb/const.py @@ -37,7 +37,7 @@ ) DOMAIN = "opencwb" -DEFAULT_NAME = "OpenCWB" +DEFAULT_NAME = "OpenCWA" DEFAULT_LANGUAGE = "zh_tw" ATTRIBUTION = "Data provided by Opendata CWA" MANUFACTURER = "OpenCWA (\u4e2d\u592e\u6c23\u8c61\u5c40\u6c23\u8c61\u8cc7\u6599\u958b\u653e\u5e73\u81fa)" diff --git a/custom_components/opencwb/core/utils/opendata_cwb.py b/custom_components/opencwb/core/utils/opendata_cwb.py index 5ad9957..a906f03 100755 --- a/custom_components/opencwb/core/utils/opendata_cwb.py +++ b/custom_components/opencwb/core/utils/opendata_cwb.py @@ -80,19 +80,19 @@ def _get_weather(the_dict, index, wx_index, last_pop, mode): value["main"]["feels_like"] = int(element_value[0][value_str]) else: value["main"]["feels_like"] = int(list(element_value[0].values())[0]) - elif "MaxAT" == i[elementname]: + elif "MaxAT" == i[elementname] or "\u6700\u9ad8\u9ad4\u611f\u6eab\u5ea6" == i[elementname]: if value_str in element_value[0]: value["main"]["feels_like"] = int(element_value[0][value_str]) value["feels_like"]["max"] = int(element_value[0][value_str]) else: value["main"]["feels_like"] = int(list(element_value[0].vaules())[0]) value["feels_like"]["max"] = int(list(element_value[0].values())[0]) - elif "MinAT" == i[elementname]: + elif "MinAT" == i[elementname] or "\u6700\u4f4e\u9ad4\u611f\u6eab\u5ea6" == i[elementname]: if value_str in element_value[0]: value["feels_like"]["min"] = int(element_value[0][value_str]) else: value["feels_like"]["min"] = int(list(element_value[0].values())[0]) - elif "UVI" == i[elementname]: + elif "UVI" == i[elementname] or "\u7d2b\u5916\u7dda\u6307\u6578" == i[elementname]: value["uvi"] = 0 for j in i["time"]: if start_time == j[starttime]: @@ -106,12 +106,12 @@ def _get_weather(the_dict, index, wx_index, last_pop, mode): value["main"]["temp"] = int(element_value[0][value_str]) else: value["main"]["temp"] = int(list(element_value[0].values())[0]) - elif "MaxT" == i[elementname]: + elif "MaxT" == i[elementname] or "\u6700\u9ad8\u6eab\u5ea6" == i[elementname]: if value_str in element_value[0]: value["main"]["temp_max"] = int(element_value[0][value_str]) else: value["main"]["temp_max"] = int(list(element_value[0].values())[0]) - elif "MinT" == i[elementname]: + elif "MinT" == i[elementname] or "\u6700\u4f4e\u6eab\u5ea6" == i[elementname]: if value_str in element_value[0]: value["main"]["temp_min"] = int(element_value[0][value_str]) else: diff --git a/custom_components/opencwb/sensor.py b/custom_components/opencwb/sensor.py index c66ce08..e79ab7b 100644 --- a/custom_components/opencwb/sensor.py +++ b/custom_components/opencwb/sensor.py @@ -2,6 +2,7 @@ from .abstract_ocwb_sensor import AbstractOpenCWBSensor from .const import ( ATTR_API_FORECAST, + CONF_LOCATION_NAME, DOMAIN, ENTRY_NAME, ENTRY_WEATHER_COORDINATOR, @@ -24,10 +25,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): entities = [] for sensor_type in MONITORED_CONDITIONS: - unique_id = f"{config_entry.unique_id}-{sensor_type}" + unique_id = f"{config_entry.unique_id}-{sensor_type}-{location_name}" entities.append( OpenCWBSensor( - f"{name} {sensor_type}", + f"{name} {location_name} {sensor_type}", unique_id, sensor_type, weather_sensor_types[sensor_type], @@ -36,10 +37,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): ) for sensor_type in FORECAST_MONITORED_CONDITIONS: - unique_id = f"{config_entry.unique_id}-forecast-{sensor_type}" + unique_id = f"{config_entry.unique_id}-forecast-{sensor_type}-{location_name}" entities.append( OpenCWBForecastSensor( - f"{name} Forecast {sensor_type}", + f"{name} {location_name} Forecast {sensor_type}", unique_id, sensor_type, forecast_sensor_types[sensor_type], diff --git a/custom_components/opencwb/weather.py b/custom_components/opencwb/weather.py index 8cced69..d88f11d 100755 --- a/custom_components/opencwb/weather.py +++ b/custom_components/opencwb/weather.py @@ -21,6 +21,7 @@ ATTR_API_WIND_GUST, ATTR_API_WIND_SPEED, ATTRIBUTION, + CONF_LOCATION_NAME, DEFAULT_NAME, DOMAIN, ENTRY_NAME, @@ -41,9 +42,10 @@ async def async_setup_entry( domain_data = hass.data[DOMAIN][config_entry.entry_id] name = domain_data[ENTRY_NAME] weather_coordinator = domain_data[ENTRY_WEATHER_COORDINATOR] + location_name = domain_data[CONF_LOCATION_NAME] unique_id = f"{config_entry.unique_id}" - ocwb_weather = OpenCWBWeather(name, unique_id, weather_coordinator) + ocwb_weather = OpenCWBWeather(f"{name} {location_name}", f"{unique_id}-{location_name}", weather_coordinator) async_add_entities([ocwb_weather], False)