Skip to content

Commit

Permalink
Change the name and the unique_id of the entities
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunglung committed Dec 14, 2024
1 parent d345c0c commit 9d1ffe4
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions custom_components/opencwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/opencwb/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -93,7 +93,7 @@
FORECAST_MODE_HOURLY,
FORECAST_MODE_DAILY,
FORECAST_MODE_ONECALL_HOURLY,
FORECAST_MODE_ONECALL_DAILY,
# FORECAST_MODE_ONECALL_DAILY,
]
DEFAULT_FORECAST_MODE = FORECAST_MODE_ONECALL_DAILY

Expand Down
10 changes: 5 additions & 5 deletions custom_components/opencwb/core/utils/opendata_cwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions custom_components/opencwb/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,16 +19,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
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]

weather_sensor_types = WEATHER_SENSOR_TYPES
forecast_sensor_types = FORECAST_SENSOR_TYPES

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],
Expand All @@ -36,10 +38,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],
Expand Down
2 changes: 1 addition & 1 deletion custom_components/opencwb/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"mode": "Mode",
"name": "Name of the integration"
},
"description": "Set up OpenCWB integration. To generate API key go to https://opendata.cwb.gov.tw/devManual/insrtuction",
"description": "Set up OpenCWB integration. To generate API key go to https://opendata.cwa.gov.tw/devManual/insrtuction. About Location Name, please check https://opendata.cwa.gov.tw/opendatadoc/Opendata_City.pdf",
"title": "OpenCWB"
}
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/opencwb/translations/zh-Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"mode": "\u6a21\u5f0f",
"name": "\u6574\u5408\u540d\u7a31"
},
"description": "\u6b32\u8a2d\u5b9a OpenCWB \u6574\u5408\u3002\u8acb\u81f3 https://opendata.cwb.gov.tw/devManual/insrtuction \u7522\u751f API \u5bc6\u9470",
"description": "\u6b32\u8a2d\u5b9a OpenCWB \u6574\u5408\u3002\u8acb\u81f3 https://opendata.cwa.gov.tw/devManual/insrtuction \u7522\u751f API \u5bc6\u9470. \u95dc\u65bc \u9109\u93ae\u540d\u7a31, \u8acb\u53c3\u95b1 https://opendata.cwa.gov.tw/opendatadoc/Opendata_City.pdf",
"title": "OpenCWB"
}
}
Expand Down
7 changes: 5 additions & 2 deletions custom_components/opencwb/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ATTR_API_WIND_GUST,
ATTR_API_WIND_SPEED,
ATTRIBUTION,
CONF_LOCATION_NAME,
DEFAULT_NAME,
DOMAIN,
ENTRY_NAME,
Expand All @@ -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)

Expand All @@ -62,9 +64,10 @@ def __init__(
self._attr_name = name
self._attr_unique_id = unique_id
self._weather_coordinator = weather_coordinator
split_unique_id = unique_id.split("-")
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, unique_id)},
identifiers={(DOMAIN, f"{split_unique_id[0]}-{split_unique_id[1]}")},
manufacturer=MANUFACTURER,
name=DEFAULT_NAME,
)
Expand Down

0 comments on commit 9d1ffe4

Please sign in to comment.