Skip to content

Commit

Permalink
Add github workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunglung committed Jun 23, 2021
1 parent 27403b2 commit 84f7801
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 108 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Validate

on:
push:
pull_request:

jobs:
spellcheck_lint:
name: ShellCheck
runs-on: ubuntu-latest
steps:
- name: 📦 Checkout the repository
uses: actions/checkout@v2

- name: ✅ Run PySpelling as a GitHub action
continue-on-error: true
uses: rojopolis/[email protected]
with:
config_path: spellcheck.yml

python_linting:
name: Python linting
runs-on: ubuntu-latest
steps:
- name: 📦 Check out source repository
uses: actions/checkout@v2
- name: 📦 Set up Python environment
uses: actions/setup-python@v1
with:
python-version: "3.8"
- name: ✅ flake8 Lint
continue-on-error: true
uses: py-actions/flake8@v1

validate:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- name: HACS validation
continue-on-error: true
uses: "hacs/action@main"
with:
category: "integration"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 osk2
Copyright (c) 2021 tsunglung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 11 additions & 7 deletions custom_components/opencwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
from .const import (
CONF_LANGUAGE,
CONF_LOCATION_NAME,
CONFIG_FLOW_VERSION,
# CONFIG_FLOW_VERSION,
DEFAULT_FORECAST_MODE,
DEFAULT_LANGUAGE,
DOMAIN,
ENTRY_NAME,
ENTRY_WEATHER_COORDINATOR,
FORECAST_MODE_FREE_DAILY,
FORECAST_MODE_ONECALL_DAILY,
# FORECAST_MODE_FREE_DAILY,
# FORECAST_MODE_ONECALL_DAILY,
PLATFORMS,
UPDATE_LISTENER,
)
Expand All @@ -47,7 +47,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
location_name = config_entry.data.get(CONF_LOCATION_NAME, None)
latitude = config_entry.data.get(CONF_LATITUDE, hass.config.latitude)
longitude = config_entry.data.get(CONF_LONGITUDE, hass.config.longitude)
forecast_mode = _get_config_value(config_entry, CONF_MODE, DEFAULT_FORECAST_MODE)
forecast_mode = _get_config_value(
config_entry, CONF_MODE, DEFAULT_FORECAST_MODE)
language = _get_config_value(config_entry, CONF_LANGUAGE, DEFAULT_LANGUAGE)

config_dict = _get_ocwb_config(language)
Expand All @@ -67,7 +68,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):

for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
hass.config_entries.async_forward_entry_setup(
config_entry, platform)
)

update_listener = config_entry.add_update_listener(async_update_options)
Expand All @@ -86,13 +88,15 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, platform)
hass.config_entries.async_forward_entry_unload(
config_entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok:
update_listener = hass.data[DOMAIN][config_entry.entry_id][UPDATE_LISTENER]
update_listener = hass.data[
DOMAIN][config_entry.entry_id][UPDATE_LISTENER]
update_listener()
hass.data[DOMAIN].pop(config_entry.entry_id)

Expand Down
3 changes: 2 additions & 1 deletion custom_components/opencwb/abstract_ocwb_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def device_info(self):
"""Return the device info."""
split_unique_id = self._unique_id.split("-")
return {
"identifiers": {(DOMAIN, f"{split_unique_id[0]}-{split_unique_id[1]}")},
"identifiers": {
(DOMAIN, f"{split_unique_id[0]}-{split_unique_id[1]}")},
"name": DEFAULT_NAME,
"manufacturer": MANUFACTURER,
"entry_type": "service",
Expand Down
58 changes: 32 additions & 26 deletions custom_components/opencwb/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
CONF_NAME,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv

from .const import (
CONF_LANGUAGE,
CONF_LOCATION_NAME,
CONFIG_FLOW_VERSION,
DEFAULT_FORECAST_MODE,
DEFAULT_LANGUAGE,
DEFAULT_NAME,
DOMAIN,
FORECAST_MODES,
FORECAST_MODE_ONECALL_HOURLY,
FORECAST_MODE_ONECALL_DAILY,
LANGUAGES,
FORECAST_MODE_ONECALL_DAILY
)
from .core.weatherapi12.uris import ONE_CALL_URI

Expand All @@ -48,16 +44,20 @@ async def async_step_user(self, user_input=None):
errors = {}

if user_input is not None:
latitude = user_input.get(CONF_LATITUDE, self.hass.config.latitude)
longitude = user_input.get(CONF_LONGITUDE, self.hass.config.longitude)
latitude = user_input.get(
CONF_LATITUDE, self.hass.config.latitude)
longitude = user_input.get(
CONF_LONGITUDE, self.hass.config.longitude)
location_name = user_input.get(CONF_LOCATION_NAME, None)
location_id = _is_supported_city(user_input[CONF_API_KEY], location_name)
location_id = _is_supported_city(
user_input[CONF_API_KEY], location_name)

if location_name and location_id is None:
errors["base"] = "invalid_location_name"
else:
await self.async_set_unique_id(
urllib.parse.quote_plus(location_name) + "-" + user_input[CONF_MODE])
urllib.parse.quote_plus(
location_name) + "-" + user_input[CONF_MODE])
self._abort_if_unique_id_configured()

if (location_id != ONE_CALL_URI and
Expand All @@ -66,7 +66,11 @@ async def async_step_user(self, user_input=None):

try:
api_online = await _is_ocwb_api_online(
self.hass, user_input[CONF_API_KEY], latitude, longitude, location_name
self.hass,
user_input[CONF_API_KEY],
latitude,
longitude,
location_name
)
if not api_online:
errors["base"] = "invalid_api_key"
Expand All @@ -85,22 +89,23 @@ async def async_step_user(self, user_input=None):
vol.Required(CONF_API_KEY): str,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
vol.Required(CONF_LOCATION_NAME): str,
# vol.Optional(
# CONF_LATITUDE, default=self.hass.config.latitude
# ): cv.latitude,
# vol.Optional(
# CONF_LONGITUDE, default=self.hass.config.longitude
# ): cv.longitude,
# vol.Optional(
# CONF_LATITUDE, default=self.hass.config.latitude
# ): cv.latitude,
# vol.Optional(
# CONF_LONGITUDE, default=self.hass.config.longitude
# ): cv.longitude,
vol.Optional(CONF_MODE, default=DEFAULT_FORECAST_MODE): vol.In(
FORECAST_MODES
),
# vol.Optional(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): vol.In(
# LANGUAGES
# ),
# vol.Optional(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): vol.In(
# LANGUAGES
# ),
}
)

return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
return self.async_show_form(
step_id="user", data_schema=schema, errors=errors)


class OpenCWBOptionsFlow(config_entries.OptionsFlow):
Expand Down Expand Up @@ -136,12 +141,12 @@ def _get_options_schema(self):
CONF_MODE, DEFAULT_FORECAST_MODE
),
): vol.In(FORECAST_MODES),
# vol.Optional(
# CONF_LANGUAGE,
# default=self.config_entry.options.get(
# CONF_LANGUAGE, DEFAULT_LANGUAGE
# ),
# ): vol.In(LANGUAGES),
# vol.Optional(
# CONF_LANGUAGE,
# default=self.config_entry.options.get(
# ONF_LANGUAGE, DEFAULT_LANGUAGE
# ),
# ): vol.In(LANGUAGES),
}
)

Expand All @@ -150,6 +155,7 @@ async def _is_ocwb_api_online(hass, api_key, lat, lon, loc):
ocwb = OCWB(api_key).weather_manager()
return await hass.async_add_executor_job(ocwb.one_call, lat, lon, loc)


def _is_supported_city(api_key, loc):
ocwb = OCWB(api_key).weather_manager()
return ocwb.supported_city(loc)
65 changes: 34 additions & 31 deletions custom_components/opencwb/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Consts for the OpenCWB."""
# pylint: disable=line too long
from homeassistant.components.weather import (
ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_EXCEPTIONAL,
Expand All @@ -17,22 +18,22 @@
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_PRESSURE,
# ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
# ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
)
from homeassistant.const import (
DEGREE,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_PRESSURE,
# DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
LENGTH_MILLIMETERS,
PERCENTAGE,
PRESSURE_HPA,
# PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
UV_INDEX,
Expand Down Expand Up @@ -93,11 +94,11 @@
ATTR_API_WIND_SPEED,
ATTR_API_WIND_BEARING,
ATTR_API_HUMIDITY,
# ATTR_API_PRESSURE,
# ATTR_API_PRESSURE,
ATTR_API_CLOUDS,
# ATTR_API_RAIN,
# ATTR_API_SNOW,
# ATTR_API_PRECIPITATION_KIND,
# ATTR_API_RAIN,
# ATTR_API_SNOW,
# ATTR_API_PRECIPITATION_KIND,
ATTR_API_UV_INDEX,
ATTR_API_CONDITION,
ATTR_API_WEATHER_CODE,
Expand All @@ -106,9 +107,9 @@
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
# ATTR_FORECAST_PRESSURE,
# ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
# ATTR_FORECAST_TEMP_LOW,
# ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
Expand All @@ -119,14 +120,16 @@
]
WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT = 0
CONDITION_CLASSES = {
ATTR_CONDITION_CLOUDY: [ 7],
ATTR_CONDITION_FOG: [24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 38, 39, 41],
ATTR_CONDITION_CLOUDY: [7],
ATTR_CONDITION_FOG: [
24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 38, 39, 41],
ATTR_CONDITION_HAIL: [42],
ATTR_CONDITION_LIGHTNING: [15, 16, 17],
ATTR_CONDITION_LIGHTNING_RAINY: [18],
ATTR_CONDITION_PARTLYCLOUDY: [4, 5, 6],
ATTR_CONDITION_POURING: [18],
ATTR_CONDITION_RAINY: [8, 9, 10, 11, 12, 13, 14, 19, 20, 21, 22, 29, 30, 31],
ATTR_CONDITION_RAINY: [
8, 9, 10, 11, 12, 13, 14, 19, 20, 21, 22, 29, 30, 31],
ATTR_CONDITION_SNOWY: [42],
ATTR_CONDITION_SNOWY_RAINY: [23, 37],
ATTR_CONDITION_SUNNY: [1],
Expand Down Expand Up @@ -163,15 +166,15 @@
SENSOR_UNIT: PERCENTAGE,
SENSOR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
},
# ATTR_API_PRESSURE: {
# SENSOR_NAME: "Pressure",
# SENSOR_UNIT: PRESSURE_HPA,
# SENSOR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
# },
# ATTR_API_PRESSURE: {
# SENSOR_NAME: "Pressure",
# SENSOR_UNIT: PRESSURE_HPA,
# SENSOR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
# },
ATTR_API_CLOUDS: {SENSOR_NAME: "Cloud coverage", SENSOR_UNIT: PERCENTAGE},
# ATTR_API_RAIN: {SENSOR_NAME: "Rain", SENSOR_UNIT: LENGTH_MILLIMETERS},
# ATTR_API_SNOW: {SENSOR_NAME: "Snow", SENSOR_UNIT: LENGTH_MILLIMETERS},
# ATTR_API_PRECIPITATION_KIND: {SENSOR_NAME: "Precipitation kind"},
# ATTR_API_RAIN: {SENSOR_NAME: "Rain", SENSOR_UNIT: LENGTH_MILLIMETERS},
# ATTR_API_SNOW: {SENSOR_NAME: "Snow", SENSOR_UNIT: LENGTH_MILLIMETERS},
# ATTR_API_PRECIPITATION_KIND: {SENSOR_NAME: "Precipitation kind"},
ATTR_API_UV_INDEX: {
SENSOR_NAME: "UV Index",
SENSOR_UNIT: UV_INDEX,
Expand All @@ -189,21 +192,21 @@
SENSOR_NAME: "Precipitation probability",
SENSOR_UNIT: PERCENTAGE,
},
# ATTR_FORECAST_PRESSURE: {
# SENSOR_NAME: "Pressure",
# SENSOR_UNIT: PRESSURE_HPA,
# SENSOR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
# },
# ATTR_FORECAST_PRESSURE: {
# SENSOR_NAME: "Pressure",
# SENSOR_UNIT: PRESSURE_HPA,
# SENSOR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
# },
ATTR_FORECAST_TEMP: {
SENSOR_NAME: "Temperature",
SENSOR_UNIT: TEMP_CELSIUS,
SENSOR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
},
# ATTR_FORECAST_TEMP_LOW: {
# SENSOR_NAME: "Temperature Low",
# SENSOR_UNIT: TEMP_CELSIUS,
# SENSOR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
# },
# ATTR_FORECAST_TEMP_LOW: {
# SENSOR_NAME: "Temperature Low",
# SENSOR_UNIT: TEMP_CELSIUS,
# SENSOR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
# },
ATTR_FORECAST_TIME: {
SENSOR_NAME: "Time",
SENSOR_DEVICE_CLASS: DEVICE_CLASS_TIMESTAMP,
Expand Down
Loading

0 comments on commit 84f7801

Please sign in to comment.