mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Fix temperature unit conversion in Ambient PWS (#20723)
This commit is contained in:
parent
ce05af2720
commit
a05031c22e
@ -11,7 +11,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.config_entries import SOURCE_IMPORT
|
from homeassistant.config_entries import SOURCE_IMPORT
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_NAME, ATTR_LOCATION, CONF_API_KEY, CONF_MONITORED_CONDITIONS,
|
ATTR_NAME, ATTR_LOCATION, CONF_API_KEY, CONF_MONITORED_CONDITIONS,
|
||||||
CONF_UNIT_SYSTEM, EVENT_HOMEASSISTANT_STOP)
|
EVENT_HOMEASSISTANT_STOP)
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
@ -19,8 +19,7 @@ from homeassistant.helpers.event import async_call_later
|
|||||||
|
|
||||||
from .config_flow import configured_instances
|
from .config_flow import configured_instances
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_LAST_DATA, CONF_APP_KEY, DATA_CLIENT, DOMAIN, TOPIC_UPDATE, UNITS_SI,
|
ATTR_LAST_DATA, CONF_APP_KEY, DATA_CLIENT, DOMAIN, TOPIC_UPDATE)
|
||||||
UNITS_US)
|
|
||||||
|
|
||||||
REQUIREMENTS = ['aioambient==0.1.0']
|
REQUIREMENTS = ['aioambient==0.1.0']
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -28,36 +27,36 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
DEFAULT_SOCKET_MIN_RETRY = 15
|
DEFAULT_SOCKET_MIN_RETRY = 15
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'24hourrainin': ['24 Hr Rain', 'in'],
|
'24hourrainin': ('24 Hr Rain', 'in'),
|
||||||
'baromabsin': ['Abs Pressure', 'inHg'],
|
'baromabsin': ('Abs Pressure', 'inHg'),
|
||||||
'baromrelin': ['Rel Pressure', 'inHg'],
|
'baromrelin': ('Rel Pressure', 'inHg'),
|
||||||
'battout': ['Battery', ''],
|
'battout': ('Battery', ''),
|
||||||
'co2': ['co2', 'ppm'],
|
'co2': ('co2', 'ppm'),
|
||||||
'dailyrainin': ['Daily Rain', 'in'],
|
'dailyrainin': ('Daily Rain', 'in'),
|
||||||
'dewPoint': ['Dew Point', ['°F', '°C']],
|
'dewPoint': ('Dew Point', '°F'),
|
||||||
'eventrainin': ['Event Rain', 'in'],
|
'eventrainin': ('Event Rain', 'in'),
|
||||||
'feelsLike': ['Feels Like', ['°F', '°C']],
|
'feelsLike': ('Feels Like', '°F'),
|
||||||
'hourlyrainin': ['Hourly Rain Rate', 'in/hr'],
|
'hourlyrainin': ('Hourly Rain Rate', 'in/hr'),
|
||||||
'humidity': ['Humidity', '%'],
|
'humidity': ('Humidity', '%'),
|
||||||
'humidityin': ['Humidity In', '%'],
|
'humidityin': ('Humidity In', '%'),
|
||||||
'lastRain': ['Last Rain', ''],
|
'lastRain': ('Last Rain', ''),
|
||||||
'maxdailygust': ['Max Gust', 'mph'],
|
'maxdailygust': ('Max Gust', 'mph'),
|
||||||
'monthlyrainin': ['Monthly Rain', 'in'],
|
'monthlyrainin': ('Monthly Rain', 'in'),
|
||||||
'solarradiation': ['Solar Rad', 'W/m^2'],
|
'solarradiation': ('Solar Rad', 'W/m^2'),
|
||||||
'tempf': ['Temp', ['°F', '°C']],
|
'tempf': ('Temp', '°F'),
|
||||||
'tempinf': ['Inside Temp', ['°F', '°C']],
|
'tempinf': ('Inside Temp', '°F'),
|
||||||
'totalrainin': ['Lifetime Rain', 'in'],
|
'totalrainin': ('Lifetime Rain', 'in'),
|
||||||
'uv': ['uv', 'Index'],
|
'uv': ('uv', 'Index'),
|
||||||
'weeklyrainin': ['Weekly Rain', 'in'],
|
'weeklyrainin': ('Weekly Rain', 'in'),
|
||||||
'winddir': ['Wind Dir', '°'],
|
'winddir': ('Wind Dir', '°'),
|
||||||
'winddir_avg10m': ['Wind Dir Avg 10m', '°'],
|
'winddir_avg10m': ('Wind Dir Avg 10m', '°'),
|
||||||
'winddir_avg2m': ['Wind Dir Avg 2m', 'mph'],
|
'winddir_avg2m': ('Wind Dir Avg 2m', 'mph'),
|
||||||
'windgustdir': ['Gust Dir', '°'],
|
'windgustdir': ('Gust Dir', '°'),
|
||||||
'windgustmph': ['Wind Gust', 'mph'],
|
'windgustmph': ('Wind Gust', 'mph'),
|
||||||
'windspdmph_avg10m': ['Wind Avg 10m', 'mph'],
|
'windspdmph_avg10m': ('Wind Avg 10m', 'mph'),
|
||||||
'windspdmph_avg2m': ['Wind Avg 2m', 'mph'],
|
'windspdmph_avg2m': ('Wind Avg 2m', 'mph'),
|
||||||
'windspeedmph': ['Wind Speed', 'mph'],
|
'windspeedmph': ('Wind Speed', 'mph'),
|
||||||
'yearlyrainin': ['Yearly Rain', 'in'],
|
'yearlyrainin': ('Yearly Rain', 'in'),
|
||||||
}
|
}
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
@ -70,8 +69,6 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
vol.Optional(
|
vol.Optional(
|
||||||
CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
|
CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
|
||||||
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||||
vol.Optional(CONF_UNIT_SYSTEM):
|
|
||||||
vol.In([UNITS_SI, UNITS_US]),
|
|
||||||
})
|
})
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
@ -111,8 +108,7 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
config_entry.data[CONF_API_KEY],
|
config_entry.data[CONF_API_KEY],
|
||||||
config_entry.data[CONF_APP_KEY], session),
|
config_entry.data[CONF_APP_KEY], session),
|
||||||
config_entry.data.get(
|
config_entry.data.get(
|
||||||
CONF_MONITORED_CONDITIONS, list(SENSOR_TYPES)),
|
CONF_MONITORED_CONDITIONS, list(SENSOR_TYPES)))
|
||||||
config_entry.data.get(CONF_UNIT_SYSTEM))
|
|
||||||
hass.loop.create_task(ambient.ws_connect())
|
hass.loop.create_task(ambient.ws_connect())
|
||||||
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = ambient
|
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = ambient
|
||||||
except WebsocketConnectionError as err:
|
except WebsocketConnectionError as err:
|
||||||
@ -139,9 +135,7 @@ async def async_unload_entry(hass, config_entry):
|
|||||||
class AmbientStation:
|
class AmbientStation:
|
||||||
"""Define a class to handle the Ambient websocket."""
|
"""Define a class to handle the Ambient websocket."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, hass, config_entry, client, monitored_conditions):
|
||||||
self, hass, config_entry, client, monitored_conditions,
|
|
||||||
unit_system):
|
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self._config_entry = config_entry
|
self._config_entry = config_entry
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
@ -149,7 +143,6 @@ class AmbientStation:
|
|||||||
self.client = client
|
self.client = client
|
||||||
self.monitored_conditions = monitored_conditions
|
self.monitored_conditions = monitored_conditions
|
||||||
self.stations = {}
|
self.stations = {}
|
||||||
self.unit_system = unit_system
|
|
||||||
|
|
||||||
async def ws_connect(self):
|
async def ws_connect(self):
|
||||||
"""Register handlers and connect to the websocket."""
|
"""Register handlers and connect to the websocket."""
|
||||||
|
@ -8,6 +8,3 @@ CONF_APP_KEY = 'app_key'
|
|||||||
DATA_CLIENT = 'data_client'
|
DATA_CLIENT = 'data_client'
|
||||||
|
|
||||||
TOPIC_UPDATE = 'update'
|
TOPIC_UPDATE = 'update'
|
||||||
|
|
||||||
UNITS_SI = 'si'
|
|
||||||
UNITS_US = 'us'
|
|
||||||
|
@ -12,14 +12,11 @@ from homeassistant.const import ATTR_NAME
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
|
||||||
from .const import (
|
from .const import ATTR_LAST_DATA, DATA_CLIENT, DOMAIN, TOPIC_UPDATE
|
||||||
ATTR_LAST_DATA, DATA_CLIENT, DOMAIN, TOPIC_UPDATE, UNITS_SI, UNITS_US)
|
|
||||||
|
|
||||||
DEPENDENCIES = ['ambient_station']
|
DEPENDENCIES = ['ambient_station']
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
UNIT_SYSTEM = {UNITS_US: 0, UNITS_SI: 1}
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
hass, config, async_add_entities, discovery_info=None):
|
hass, config, async_add_entities, discovery_info=None):
|
||||||
@ -31,20 +28,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
"""Set up an Ambient PWS sensor based on a config entry."""
|
"""Set up an Ambient PWS sensor based on a config entry."""
|
||||||
ambient = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
ambient = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
||||||
|
|
||||||
if ambient.unit_system:
|
|
||||||
sys_units = ambient.unit_system
|
|
||||||
elif hass.config.units.is_metric:
|
|
||||||
sys_units = UNITS_SI
|
|
||||||
else:
|
|
||||||
sys_units = UNITS_US
|
|
||||||
|
|
||||||
sensor_list = []
|
sensor_list = []
|
||||||
for mac_address, station in ambient.stations.items():
|
for mac_address, station in ambient.stations.items():
|
||||||
for condition in ambient.monitored_conditions:
|
for condition in ambient.monitored_conditions:
|
||||||
name, unit = SENSOR_TYPES[condition]
|
name, unit = SENSOR_TYPES[condition]
|
||||||
if isinstance(unit, list):
|
|
||||||
unit = unit[UNIT_SYSTEM[sys_units]]
|
|
||||||
|
|
||||||
sensor_list.append(
|
sensor_list.append(
|
||||||
AmbientWeatherSensor(
|
AmbientWeatherSensor(
|
||||||
ambient, mac_address, station[ATTR_NAME], condition, name,
|
ambient, mac_address, station[ATTR_NAME], condition, name,
|
||||||
@ -58,7 +45,7 @@ class AmbientWeatherSensor(Entity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, ambient, mac_address, station_name, sensor_type, sensor_name,
|
self, ambient, mac_address, station_name, sensor_type, sensor_name,
|
||||||
units):
|
unit):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._ambient = ambient
|
self._ambient = ambient
|
||||||
self._async_unsub_dispatcher_connect = None
|
self._async_unsub_dispatcher_connect = None
|
||||||
@ -67,7 +54,7 @@ class AmbientWeatherSensor(Entity):
|
|||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self._state = None
|
self._state = None
|
||||||
self._station_name = station_name
|
self._station_name = station_name
|
||||||
self._units = units
|
self._unit = unit
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -87,7 +74,7 @@ class AmbientWeatherSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit of measurement."""
|
"""Return the unit of measurement."""
|
||||||
return self._units
|
return self._unit
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user