Skip to content

Commit

Permalink
Merge pull request #17 from pawkakol1/feature/temperaureUnits
Browse files Browse the repository at this point in the history
Temperature Unit choosing - added
  • Loading branch information
pawkakol1 authored Jun 13, 2022
2 parents de0293f + 75573d8 commit 5a0c623
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
20 changes: 13 additions & 7 deletions custom_components/worlds_air_quality_index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import (
callback,
HomeAssistant
)
from homeassistant.helpers.entity_registry import async_migrate_entries
from homeassistant.const import (
CONF_NAME,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_TOKEN,
CONF_LOCATION,
CONF_METHOD,
CONF_ID
CONF_ID,
CONF_TEMPERATURE_UNIT,
TEMP_CELSIUS
)

from .const import (
PLATFORMS,
GEOGRAPHIC_LOCALIZATION
PLATFORMS
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -70,6 +67,15 @@ async def async_migrate_entry(hass, config_entry):
version = 2
config_entry.version = version
config_entries.async_update_entry(config_entry, data=new_data)

if version == 2:
tempUnit = TEMP_CELSIUS
new_data = {**data, CONF_TEMPERATURE_UNIT: tempUnit}

version = 3
config_entry.version = version
config_entries.async_update_entry(config_entry, data=new_data)


_LOGGER.info("Migration to version %s successful", version)

Expand Down
24 changes: 22 additions & 2 deletions custom_components/worlds_air_quality_index/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
CONF_TOKEN,
CONF_LOCATION,
CONF_METHOD,
CONF_ID
CONF_ID,
CONF_TEMPERATURE_UNIT,
TEMP_FAHRENHEIT,
TEMP_CELSIUS
)
from .const import (
DOMAIN,
Expand All @@ -31,7 +34,7 @@
class WorldsAirQualityIndexConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for worlds_air_quality_index integration."""

VERSION = 2
VERSION = 3

async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
"""Import a configuration from config.yaml."""
Expand All @@ -52,6 +55,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo
STATION_ID
)
)

}
)

Expand All @@ -72,6 +76,12 @@ async def async_step_geographic_localization(self, user_input=None) -> FlowResul
data_schema = vol.Schema(
{
vol.Required(CONF_TOKEN): cv.string,
vol.Required(CONF_TEMPERATURE_UNIT, default=TEMP_CELSIUS): vol.In(
(
TEMP_CELSIUS,
TEMP_FAHRENHEIT
)
),
vol.Required(CONF_LATITUDE, default=self.hass.config.latitude): cv.latitude,
vol.Required(CONF_LONGITUDE, default=self.hass.config.longitude): cv.longitude,
vol.Optional(CONF_NAME): cv.string
Expand All @@ -80,6 +90,7 @@ async def async_step_geographic_localization(self, user_input=None) -> FlowResul

if user_input:
token = user_input[CONF_TOKEN]
tempUnit = user_input[CONF_TEMPERATURE_UNIT]
latitude = user_input[CONF_LATITUDE]
longitude = user_input[CONF_LONGITUDE]
method = CONF_LOCATION
Expand Down Expand Up @@ -116,6 +127,7 @@ async def async_step_geographic_localization(self, user_input=None) -> FlowResul
title=name,
data={
CONF_TOKEN: token,
CONF_TEMPERATURE_UNIT: tempUnit,
CONF_LATITUDE: latitude,
CONF_LONGITUDE: longitude,
CONF_NAME: name,
Expand All @@ -135,6 +147,12 @@ async def async_step_station_id(self, user_input=None) -> FlowResult:
data_schema = vol.Schema(
{
vol.Required(CONF_TOKEN): cv.string,
vol.Required(CONF_TEMPERATURE_UNIT, default=TEMP_CELSIUS): vol.In(
(
TEMP_CELSIUS,
TEMP_FAHRENHEIT
)
),
vol.Required(CONF_ID): cv.string,
vol.Optional(CONF_NAME): cv.string
}
Expand All @@ -143,6 +161,7 @@ async def async_step_station_id(self, user_input=None) -> FlowResult:
if user_input:

token = user_input[CONF_TOKEN]
tempUnit = user_input[CONF_TEMPERATURE_UNIT]
id = user_input[CONF_ID]
method = CONF_ID
requester = WaqiDataRequester(None, None, token, id, method)
Expand Down Expand Up @@ -178,6 +197,7 @@ async def async_step_station_id(self, user_input=None) -> FlowResult:
title=name,
data={
CONF_TOKEN: token,
CONF_TEMPERATURE_UNIT: tempUnit,
CONF_ID: id,
CONF_NAME: name,
CONF_METHOD: method,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/worlds_air_quality_index/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

DOMAIN = "worlds_air_quality_index"
PLATFORMS = [Platform.SENSOR]
SW_VERSION = "0.3.3"
SW_VERSION = "0.3.4"

SCAN_INTERVAL = timedelta(minutes=30)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/worlds_air_quality_index/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"dependencies": [],
"codeowners": ["@pawkakol1"],
"iot_class": "cloud_polling",
"version": "0.3.3"
"version": "0.3.4"
}
34 changes: 20 additions & 14 deletions custom_components/worlds_air_quality_index/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
from __future__ import annotations

import logging
from this import s

from .waqi_api import WaqiDataRequester

import json

import voluptuous as vol

from homeassistant.components.sensor import (
Expand All @@ -26,7 +29,10 @@
CONF_LONGITUDE,
CONF_TOKEN,
CONF_ID,
CONF_METHOD
CONF_METHOD,
CONF_TEMPERATURE_UNIT,
TEMP_FAHRENHEIT,
TEMP_CELSIUS
)

from .const import (
Expand All @@ -38,16 +44,6 @@

_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_TOKEN): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_LATITUDE): cv.string,
vol.Optional(CONF_LONGITUDE): cv.string,
}
)


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
Expand Down Expand Up @@ -84,6 +80,7 @@ async def async_setup_entry(
name = entry.data[CONF_NAME]
token = entry.data[CONF_TOKEN]
method = entry.data[CONF_METHOD]
tempUnit = entry.data[CONF_TEMPERATURE_UNIT]

if method == CONF_ID:
_LOGGER.debug("config ID:")
Expand All @@ -108,7 +105,7 @@ async def async_setup_entry(

for res in SENSORS:
if res == "aqi" or res in scannedData:
entities.append(WorldsAirQualityIndexSensor(res, requester))
entities.append(WorldsAirQualityIndexSensor(res, requester, tempUnit))

async_add_entities(entities, update_before_add=True)

Expand All @@ -117,14 +114,15 @@ async def async_setup_entry(
class WorldsAirQualityIndexSensor(SensorEntity):
"""Representation of a Sensor."""

def __init__(self, resType: str, requester: WaqiDataRequester) -> None:
def __init__(self, resType: str, requester: WaqiDataRequester, tempUnit: str) -> None:
self._state = None
self._resType = resType
self._requester = requester
self._stationName = self._requester.GetStationName()
self._stationIdx = self._requester.GetStationIdx()
self._updateLastTime = self._requester.GetUpdateLastTime()
self._name = SENSORS[self._resType][0]
self._tempUnit = tempUnit

self._attr_name = self._name
self._attr_unique_id = f"{self._stationName}_{self._stationIdx}_{self._name}"
Expand All @@ -148,7 +146,10 @@ def state(self):
@property
def unit_of_measurement(self) -> str:
#Return the unit of measurement.
return SENSORS[self._resType][1]
if SENSORS[self._resType][1] == TEMP_CELSIUS:
return self._tempUnit
else:
return SENSORS[self._resType][1]

@property
def device_class(self) -> SensorDeviceClass | str | None:
Expand All @@ -169,6 +170,11 @@ def update(self) -> None:

if self._resType == 'aqi':
self._state = float(_data["data"]["aqi"])
elif self._resType == 't':
if self._tempUnit == TEMP_FAHRENHEIT:
self._state = 9.0 * float(_data["data"]["iaqi"]['t']["v"]) / 5.0 + 32.0
else:
self._state = float(_data["data"]["iaqi"]['t']["v"])
else:
self._state = float(_data["data"]["iaqi"][self._resType]["v"])

Expand Down

0 comments on commit 5a0c623

Please sign in to comment.