Skip to content

Commit

Permalink
add weather entity and extra wind dir sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
tlskinneriv committed Sep 10, 2023
1 parent e44a35a commit 2297d4f
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ WORKDIR /srv/homeassistant
RUN python -m venv . \
&& source bin/activate \
&& python -m pip install wheel \
&& pip3 install homeassistant
&& pip3 install urllib3==1.26.7 homeassistant

RUN ./setup.sh

Expand Down
18 changes: 4 additions & 14 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@
"vscode": {
"settings": {
"python.defaultInterpreterPath": "/srv/homeassistant/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.formatting.provider": "black",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint",
"python.linting.pylintArgs": [
"pylint.path": ["/usr/local/py-utils/bin/pylint"],
"pylint.args": [
"--init-hook",
"import sys; sys.path.append('/srv/homeassistant/lib/python3.11/site-packages/')"
],
Expand All @@ -51,7 +40,8 @@
"esbenp.prettier-vscode",
"GitHub.vscode-pull-request-github",
"streetsidesoftware.code-spell-checker",
"ms-python.pylint"
"ms-python.pylint",
"ms-python.black-formatter"
]
}
},
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"[python]": {
"editor.defaultFormatter": "ms-python.python"
"editor.defaultFormatter": "ms-python.black-formatter"
},
"editor.rulers": [100],
"rewrap.autoWrap.enabled": true,
"rewrap.wrappingColumn": 100,
"python.formatting.provider": "black",
"cSpell.words": ["awnet", "hass", "homeassistant"]
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## [1.2.0] - 2023-09-09

### Added

- Calculated sensors for cardinal wind directions on winddir and windgustdir
- BETA: added a weather entity to collect common data in one place and loosely predict the weather
condition outside based on data available from the sensor array.

## [1.1.3] - 2023-07-13

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion custom_components/awnet_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR]
PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.WEATHER]
STORAGE_KEY = DOMAIN + "_data"
STORAGE_VERSION = 1

Expand Down
20 changes: 18 additions & 2 deletions custom_components/awnet_local/const_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@
TYPE_UV = "uv"
TYPE_WEEKLYRAININ = "weeklyrainin"
TYPE_WINDDIR = "winddir"
TYPE_WINDDIR_CARD = "winddir_card"
TYPE_WINDDIR_AVG10M = "winddir_Avg10m"
TYPE_WINDDIR_AVG2M = "winddir_Avg2m"
TYPE_WINDGUSTDIR = "windgustdir"
TYPE_WINDGUSTDIR_CARD = "windgustdir_card"
TYPE_WINDGUSTMPH = "windgustmph"
TYPE_WINDSPDMPH_AVG10M = "windspdmph_Avg10m"
TYPE_WINDSPDMPH_AVG2M = "windspdmph_Avg2m"
Expand Down Expand Up @@ -264,7 +266,9 @@
TYPE_WINDDIR_AVG10M,
TYPE_WINDDIR_AVG2M,
TYPE_WINDDIR,
TYPE_WINDDIR_CARD,
TYPE_WINDGUSTDIR,
TYPE_WINDGUSTDIR_CARD,
TYPE_WINDGUSTMPH,
TYPE_WINDSPDMPH_AVG10M,
TYPE_WINDSPDMPH_AVG2M,
Expand All @@ -282,6 +286,8 @@
TYPE_FEELSLIKE_IN: [TYPE_TEMPINF, TYPE_HUMIDITYIN],
TYPE_DEWPOINT_IN: [TYPE_TEMPINF, TYPE_HUMIDITYIN],
TYPE_LIGHTNING_PER_HOUR: [TYPE_LIGHTNING_PER_DAY, TYPE_DATEUTC],
TYPE_WINDDIR_CARD: [TYPE_WINDDIR],
TYPE_WINDGUSTDIR_CARD: [TYPE_WINDGUSTDIR],
}

# Each sensor listed here is converted server-side from the native unit to the unit that HA supports
Expand Down Expand Up @@ -1069,11 +1075,16 @@
),
SensorEntityDescription(
key=TYPE_WINDDIR,
name="Wind Dir",
name="Wind Direction",
icon="mdi:weather-windy",
native_unit_of_measurement=DEGREE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=TYPE_WINDDIR_CARD,
name="Wind Direction Cardinal",
icon="mdi:weather-windy",
),
SensorEntityDescription(
key=TYPE_WINDDIR_AVG10M,
name="Wind Dir Avg 10m",
Expand All @@ -1090,11 +1101,16 @@
),
SensorEntityDescription(
key=TYPE_WINDGUSTDIR,
name="Gust Dir",
name="Gust Direction",
icon="mdi:weather-windy",
native_unit_of_measurement=DEGREE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key=TYPE_WINDGUSTDIR_CARD,
name="Gust Direction Cardinal",
icon="mdi:weather-windy",
),
SensorEntityDescription(
key=TYPE_WINDGUSTMPH,
name="Wind Gust",
Expand Down
43 changes: 43 additions & 0 deletions custom_components/awnet_local/helpers_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
TYPE_LIGHTNING_PER_HOUR,
TYPE_LIGHTNING_PER_DAY,
TYPE_DATEUTC,
TYPE_WINDDIR,
TYPE_WINDDIR_CARD,
TYPE_WINDGUSTDIR,
TYPE_WINDGUSTDIR_CARD,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -87,6 +91,14 @@ def calculate(entity_key: str, station_data: dict[str, object]) -> object:
int(station_values.get(TYPE_LIGHTNING_PER_DAY)),
station_data[ATTR_LIGHTNING_DATA],
)
if entity_key == TYPE_WINDDIR_CARD:
return AmbientSensorCalculations.degree_to_cardinal(
float(station_values.get(TYPE_WINDDIR))
)
if entity_key == TYPE_WINDGUSTDIR_CARD:
return AmbientSensorCalculations.degree_to_cardinal(
float(station_values.get(TYPE_WINDGUSTDIR))
)
raise NotImplementedError(f"Calculation for {entity_key} is not implemented")

@staticmethod
Expand Down Expand Up @@ -291,6 +303,37 @@ def dew_point(tempf: float, rel_humid_percent: float) -> float:
dew_pt_c = (const_c * gamma_t_rh) / (const_b - gamma_t_rh)
return float(round(dew_pt_c * 9 / 5 + 32, 1))

@staticmethod
def degree_to_cardinal(direction_degree: float) -> str:
"""Converts a direction in degrees to its cardinal equivalent
Args:
direction_degree (float): Direction in degrees
Returns:
str: Cardinal direction
"""
direction = [
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW",
"N",
]
return direction[int((direction_degree + 11.25) / 22.5)]


class AmbientSensorConversions:
"""Class full of static methods for performing conversions from native units to HA units where
Expand Down
Loading

0 comments on commit 2297d4f

Please sign in to comment.