Skip to content

Commit

Permalink
Update sensor.py
Browse files Browse the repository at this point in the history
Update to create missing sensors and limit to 1 decimal
  • Loading branch information
kayjei authored Feb 3, 2021
1 parent 26c3cf3 commit 7d93457
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/badtemp_karlskrona/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
A sensor created to read temperature from swimareas in Karlskrona Sweden
For more details about this platform, please refer to the documentation at
https://github.com/kayjei/swimareas_karlskrona
https://github.com/kayjei/swimareas_karlskrona
"""
import logging
import json
Expand Down Expand Up @@ -41,7 +41,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.info("Adding sensor: " + str(id))

else:
_LOGGER.info("Skipping sensor: " + str(id) + " (temp is no float)")
devices.append(SensorDevice(id, None, lat, lon, timestamp, name))
_LOGGER.info("Adding faulty sensor: " + str(id) + " (temperature missing)")

add_devices(devices)

Expand All @@ -61,7 +62,8 @@ def update(self):
"""Temperature"""
for json in ApiRequest().json_data()["Payload"]["swimAreas"]:
if str.lower(json["nameArea"]).replace("\xe5","a").replace("\xe4","a").replace("\xf6","o") == str.lower(self._device_id):
self._state = float(json["temperatureWater"])
if self._state is not None:
self._state = float(round(json["temperatureWater"], 1))
self._latitude = str(json["geometryArea"]["y"])
self._longitude = str(json["geometryArea"]["x"])
self._timestamp = datetime.datetime.strptime(str(json["timeStamp"]).split('.')[0], "%Y-%m-%dT%H:%M:%S")
Expand Down Expand Up @@ -97,7 +99,7 @@ def state(self):
def icon(self):
"""Return the icon of the sensor"""
return 'mdi:coolant-temperature'

@property
def device_class(self):
"""Return the device class of the sensor"""
Expand Down

0 comments on commit 7d93457

Please sign in to comment.