Skip to content

Commit

Permalink
V2.0.2
Browse files Browse the repository at this point in the history
- Fix daily count issue
- Increment daily count across all instance of the integration
- Fix translation issue with selectors
  • Loading branch information
petergridge authored May 16, 2023
1 parent 55eb04d commit a75c37d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 83 deletions.
92 changes: 56 additions & 36 deletions custom_components/openweathermaphistory/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ async def async_step_add(self, user_input=None):
'''add zone'''
errors = {}
newdata = {}
measurement = None
measurement = False
string = False
newdata.update(self._data)
if user_input is not None:
for sensor in self._data.get(CONF_RESOURCES):
Expand All @@ -125,8 +126,10 @@ async def async_step_add(self, user_input=None):
break
evalform = evaluate_custom_formula(user_input.get(CONF_FORMULA),self._data.get(CONF_MAX_DAYS,0))
match evalform:
case 'MEASUREMENT':
case 'measurement':
measurement = True
case 'string':
string = True
case 'syntax':
errors[CONF_FORMULA] = "formula"
case 'undefined':
Expand All @@ -141,9 +144,11 @@ async def async_step_add(self, user_input=None):
data[CONF_NAME] = user_input[CONF_NAME]
data[CONF_FORMULA] = user_input[CONF_FORMULA]
data[CONF_ATTRIBUTES] = user_input.get(CONF_ATTRIBUTES,None)
data[CONF_SENSORCLASS] = user_input.get(CONF_SENSORCLASS,'None')
data[CONF_SENSORCLASS] = user_input.get(CONF_SENSORCLASS,',one')
if measurement is True:
data[CONF_STATECLASS] = 'MEASUREMENT'
data[CONF_STATECLASS] = 'measurement'
if string is True:
data[CONF_SENSORCLASS] = 'none'
data[CONF_UID] = str(uuid.uuid4())
newdata[CONF_RESOURCES].append(data)
self._data = newdata
Expand Down Expand Up @@ -235,14 +240,17 @@ async def async_step_modify(self, user_input=None):
errors = {}
newdata = {}
measurement = False
string = False
newdata.update(self._data)
this_sensor = newdata.get(CONF_RESOURCES)[self.selected]

if user_input is not None:
evalform = evaluate_custom_formula(user_input.get(CONF_FORMULA),self._data.get(CONF_MAX_DAYS,0))
match evalform:
case 'MEASUREMENT':
case 'measurement':
measurement = True
case 'string':
string = True
case 'syntax':
errors[CONF_FORMULA] = "formula"
case 'undefined':
Expand All @@ -257,7 +265,9 @@ async def async_step_modify(self, user_input=None):
data[CONF_SENSORCLASS] = user_input[CONF_SENSORCLASS]
data[CONF_UID] = this_sensor[CONF_UID]
if measurement is True:
data[CONF_STATECLASS] = 'MEASUREMENT'
data[CONF_STATECLASS] = 'measurement'
if string is True:
data[CONF_SENSORCLASS] = 'none'
newdata[CONF_RESOURCES][self.selected] = data
self._data = newdata
return await self.async_step_menu()
Expand All @@ -270,18 +280,18 @@ async def async_step_modify(self, user_input=None):
{
vol.Required(CONF_FORMULA,default = this_sensor.get(CONF_FORMULA,None)): sel.TemplateSelector({}),
vol.Optional(CONF_ATTRIBUTES,default = this_sensor.get(CONF_ATTRIBUTES,None)): cv.string,
vol.Optional(CONF_SENSORCLASS,default = this_sensor.get(CONF_SENSORCLASS,'None')): sel.SelectSelector(
vol.Optional(CONF_SENSORCLASS,default = this_sensor.get(CONF_SENSORCLASS,'none')): sel.SelectSelector(
sel.SelectSelectorConfig(
translation_key="sensor_class",
options=[
{"label":"None", "value":"NONE"},
{"label":"Humidity", "value":"HUMIDITY"},
{"label":"Precipitation", "value":"PRECIPITATION"},
{"label":"Precipitation Intensity", "value":"PRECIPITATIONINTENSITY"},
{"label":"Temperature", "value":"TEMPERATURE"},
{"label":"Pressure", "value":"PRESSURE"}
]
)),
translation_key="sensor_class",
options=[
{"label":"None", "value":"none"},
{"label":"Humidity", "value":"humidity"},
{"label":"Precipitation", "value":"precipitation"},
{"label":"Precipitation Intensity", "value":"precipitation_intensity"},
{"label":"Temperature", "value":"temperature"},
{"label":"Pressure", "value":"pressure"}
]
)),
}
)

Expand Down Expand Up @@ -459,14 +469,17 @@ async def async_step_modify(self, user_input=None):
errors = {}
newdata = {}
measurement = False
string = False
newdata.update(self._data)
this_sensor = newdata.get(CONF_RESOURCES)[self.selected]

if user_input is not None:
evalform = evaluate_custom_formula(user_input.get(CONF_FORMULA),self._data.get(CONF_MAX_DAYS,0))
match evalform:
case 'MEASUREMENT':
case 'measurement':
measurement = True
case 'string':
string = True
case 'syntax':
errors[CONF_FORMULA] = "formula"
case 'undefined':
Expand All @@ -480,7 +493,9 @@ async def async_step_modify(self, user_input=None):
data[CONF_ATTRIBUTES] = user_input[CONF_ATTRIBUTES]
data[CONF_SENSORCLASS] = user_input[CONF_SENSORCLASS]
if measurement is True:
data[CONF_STATECLASS] = 'MEASUREMENT'
data[CONF_STATECLASS] = 'measurement'
if string is True:
data[CONF_SENSORCLASS] = 'none'
data[CONF_UID] = this_sensor[CONF_UID]
newdata[CONF_RESOURCES][self.selected] = data
self._data = newdata
Expand All @@ -494,18 +509,18 @@ async def async_step_modify(self, user_input=None):
{
vol.Required(CONF_FORMULA,default = this_sensor.get(CONF_FORMULA,this_sensor.get(CONF_FORMULA))): sel.TemplateSelector({}),
vol.Optional(CONF_ATTRIBUTES,default = this_sensor.get(CONF_ATTRIBUTES,this_sensor.get(CONF_ATTRIBUTES,None))): cv.string,
vol.Optional(CONF_SENSORCLASS,default=this_sensor.get(CONF_SENSORCLASS,'None')): sel.SelectSelector(
sel.SelectSelectorConfig(
translation_key="sensor_class",
options=[
{"label":"None", "value":"NONE"},
{"label":"Humidity", "value":"HUMIDITY"},
{"label":"Precipitation", "value":"PRECIPITATION"},
{"label":"Precipitation Intensity", "value":"PRECIPITATIONINTENSITY"},
{"label":"Temperature", "value":"TEMPERATURE"},
{"label":"Pressure", "value":"PRESSURE"}
]
)),
vol.Optional(CONF_SENSORCLASS,default=this_sensor.get(CONF_SENSORCLASS,'none')): sel.SelectSelector(
sel.SelectSelectorConfig(
translation_key="sensor_class",
options=[
{"label":"None", "value":"none"},
{"label":"Humidity", "value":"humidity"},
{"label":"Precipitation", "value":"precipitation"},
{"label":"Precipitation Intensity", "value":"precipitation_intensity"},
{"label":"Temperature", "value":"temperature"},
{"label":"Pressure", "value":"pressure"}
]
)),
}
)

Expand All @@ -519,7 +534,8 @@ async def async_step_add(self, user_input=None):
'''add zone'''
errors = {}
newdata = {}
measurement = None
measurement = False
string = False
newdata.update(self._data)
if user_input is not None:
for sensor in self._data.get(CONF_RESOURCES):
Expand All @@ -528,8 +544,10 @@ async def async_step_add(self, user_input=None):
break
evalform = evaluate_custom_formula(user_input.get(CONF_FORMULA),self._data.get(CONF_MAX_DAYS,0))
match evalform:
case 'MEASUREMENT':
case 'measurement':
measurement = True
case 'string':
string = True
case 'syntax':
errors[CONF_FORMULA] = "formula"
case 'undefined':
Expand All @@ -543,7 +561,9 @@ async def async_step_add(self, user_input=None):
data[CONF_ATTRIBUTES] = user_input.get(CONF_ATTRIBUTES,None)
data[CONF_SENSORCLASS] = user_input.get(CONF_SENSORCLASS,None)
if measurement is True:
data[CONF_STATECLASS] = 'MEASUREMENT'
data[CONF_STATECLASS] = 'measurement'
if string is True:
data[CONF_SENSORCLASS] = 'none'
data[CONF_UID] = str(uuid.uuid4())
newdata[CONF_RESOURCES].append(data)

Expand Down Expand Up @@ -628,10 +648,10 @@ def evaluate_custom_formula(formula, max_days):
try:
templatevalue = int(templatevalue)
if isinstance(templatevalue, int):
return 'MEASUREMENT'
return 'measurement'
except ValueError:
#not a number
return
return 'string'

async def _is_owm_api_online(hass:HomeAssistant, api_key, lat, lon):
owm = OWM(api_key).weather_manager()
Expand Down
1 change: 0 additions & 1 deletion custom_components/openweathermaphistory/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
CONF_STATECLASS = "state_class"
CONF_SENSORCLASS = "sensor_class"
CONF_UID = "unique_id"
#CONF_STATE_CLASS = ["MEASUREMENT","TOTAL","TOTAL_INCREASING"]

#prevent accidental duplicate instances
CONST_PROXIMITY = 1000
Expand Down
2 changes: 1 addition & 1 deletion custom_components/openweathermaphistory/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/petergridge/openweathermaphistory/issues",
"requirements": [],
"version": "2.0.0"
"version": "2.0.2"
}
32 changes: 12 additions & 20 deletions custom_components/openweathermaphistory/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
CONF_FORMULA,
CONF_ATTRIBUTES,
CONF_INTIAL_DAYS,
CONF_PRECISION,
CONF_STATECLASS,
CONF_SENSORCLASS,
CONF_UID,
ATTRIBUTION,
DOMAIN
)

SCAN_INTERVAL = timedelta(seconds=600)
SCAN_INTERVAL = timedelta(minutes=10)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -112,19 +111,16 @@ def __init__(
super().__init__(coordinator)

self._hass = hass
self._config = config
self._state = 1
self._weather = weather
self._extra_attributes = None
self._name = resource[CONF_NAME]
self._formula = resource[CONF_FORMULA]
self._attributes = resource.get(CONF_ATTRIBUTES)
self._precision = resource.get(CONF_PRECISION,None)
self._initdays = config.get(CONF_INTIAL_DAYS)
self._sensor_class = resource.get(CONF_SENSORCLASS,None)
self._state_class = resource.get(CONF_STATECLASS,None)
self._uuid = resource.get(CONF_UID)
self._coordinator = coordinator

@callback
def _handle_coordinator_update(self) -> None:
Expand Down Expand Up @@ -156,40 +152,36 @@ def unique_id(self):
def state_class(self) -> SensorStateClass:
"""handle string instances"""
match self._state_class:
case 'MEASUREMENT':
case 'measurement':
return SensorStateClass.MEASUREMENT
case 'TOTAL':
return SensorStateClass.TOTAL
case 'TOTAL_INCREASING':
return SensorStateClass.TOTAL_INCREASING

@property
def native_unit_of_measurement(self):
match self._sensor_class:
case 'HUMIDITY':
case 'humidity':
return '%'
case 'PRECIPITATION':
case 'precipitation':
return 'mm'
case 'PRECIPITATION_INTENSITY':
case 'precipitaton_intensity':
return 'mm/h'
case 'TEMPERATURE':
case 'temperature':
return '°C'
case 'PRESSURE':
case 'pressure':
return 'hPa'

@property
def device_class(self) -> SensorDeviceClass:
"""handle string instances"""
match self._sensor_class:
case 'HUMIDITY':
case 'humidity':
return SensorDeviceClass.HUMIDITY
case 'PRECIPITATION':
case 'precipitation':
return SensorDeviceClass.PRECIPITATION
case 'PRECIPITATION_INTENSITY':
case 'precipitaton_intensity':
return SensorDeviceClass.PRECIPITATION_INTENSITY
case 'TEMPERATURE':
case 'temperature':
return SensorDeviceClass.TEMPERATURE
case 'PRESSURE':
case 'pressure':
return SensorDeviceClass.PRESSURE

@property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/openweathermaphistory/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@
}
}
}
}
}
Loading

0 comments on commit a75c37d

Please sign in to comment.