diff --git a/custom_components/worlds_air_quality_index/__init__.py b/custom_components/worlds_air_quality_index/__init__.py index f04526c..984a9c5 100644 --- a/custom_components/worlds_air_quality_index/__init__.py +++ b/custom_components/worlds_air_quality_index/__init__.py @@ -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__) @@ -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) diff --git a/custom_components/worlds_air_quality_index/config_flow.py b/custom_components/worlds_air_quality_index/config_flow.py index d34bf1b..585887f 100644 --- a/custom_components/worlds_air_quality_index/config_flow.py +++ b/custom_components/worlds_air_quality_index/config_flow.py @@ -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, @@ -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.""" @@ -52,6 +55,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo STATION_ID ) ) + } ) @@ -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 @@ -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 @@ -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, @@ -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 } @@ -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) @@ -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, diff --git a/custom_components/worlds_air_quality_index/const.py b/custom_components/worlds_air_quality_index/const.py index 3a16467..1f47692 100644 --- a/custom_components/worlds_air_quality_index/const.py +++ b/custom_components/worlds_air_quality_index/const.py @@ -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) diff --git a/custom_components/worlds_air_quality_index/manifest.json b/custom_components/worlds_air_quality_index/manifest.json index 9eb3dab..8395027 100644 --- a/custom_components/worlds_air_quality_index/manifest.json +++ b/custom_components/worlds_air_quality_index/manifest.json @@ -11,5 +11,5 @@ "dependencies": [], "codeowners": ["@pawkakol1"], "iot_class": "cloud_polling", - "version": "0.3.3" + "version": "0.3.4" } diff --git a/custom_components/worlds_air_quality_index/sensor.py b/custom_components/worlds_air_quality_index/sensor.py index b38117d..08579ee 100644 --- a/custom_components/worlds_air_quality_index/sensor.py +++ b/custom_components/worlds_air_quality_index/sensor.py @@ -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 ( @@ -26,7 +29,10 @@ CONF_LONGITUDE, CONF_TOKEN, CONF_ID, - CONF_METHOD + CONF_METHOD, + CONF_TEMPERATURE_UNIT, + TEMP_FAHRENHEIT, + TEMP_CELSIUS ) from .const import ( @@ -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, @@ -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:") @@ -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) @@ -117,7 +114,7 @@ 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 @@ -125,6 +122,7 @@ def __init__(self, resType: str, requester: WaqiDataRequester) -> None: 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}" @@ -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: @@ -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"])