Remove unit_of_measurement from climate entities (#16012)
* Remove unit_of_measurement from climate base class. * Updated google_assistant component and tests to use core temp units. * Fixes * Convert Alexa component to use core temp units for climate entities. * Fix tests. * Converted prometheus component. * Remove unit_of_measurement from homekit thermostat tests. * Small fix.
This commit is contained in:
parent
a31501d99e
commit
ae63980152
14 changed files with 184 additions and 224 deletions
|
@ -14,7 +14,6 @@ from homeassistant.components import (
|
|||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
|
@ -55,10 +54,9 @@ def register_trait(trait):
|
|||
return trait
|
||||
|
||||
|
||||
def _google_temp_unit(state):
|
||||
def _google_temp_unit(units):
|
||||
"""Return Google temperature unit."""
|
||||
if (state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) ==
|
||||
TEMP_FAHRENHEIT):
|
||||
if units == TEMP_FAHRENHEIT:
|
||||
return 'F'
|
||||
return 'C'
|
||||
|
||||
|
@ -68,8 +66,9 @@ class _Trait:
|
|||
|
||||
commands = []
|
||||
|
||||
def __init__(self, state):
|
||||
def __init__(self, hass, state):
|
||||
"""Initialize a trait for a state."""
|
||||
self.hass = hass
|
||||
self.state = state
|
||||
|
||||
def sync_attributes(self):
|
||||
|
@ -84,7 +83,7 @@ class _Trait:
|
|||
"""Test if command can be executed."""
|
||||
return command in self.commands
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute a trait command."""
|
||||
raise NotImplementedError
|
||||
|
||||
|
@ -141,24 +140,24 @@ class BrightnessTrait(_Trait):
|
|||
|
||||
return response
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute a brightness command."""
|
||||
domain = self.state.domain
|
||||
|
||||
if domain == light.DOMAIN:
|
||||
await hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
light.DOMAIN, light.SERVICE_TURN_ON, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
light.ATTR_BRIGHTNESS_PCT: params['brightness']
|
||||
}, blocking=True)
|
||||
elif domain == cover.DOMAIN:
|
||||
await hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
cover.DOMAIN, cover.SERVICE_SET_COVER_POSITION, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
cover.ATTR_POSITION: params['brightness']
|
||||
}, blocking=True)
|
||||
elif domain == media_player.DOMAIN:
|
||||
await hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
media_player.DOMAIN, media_player.SERVICE_VOLUME_SET, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
media_player.ATTR_MEDIA_VOLUME_LEVEL:
|
||||
|
@ -201,7 +200,7 @@ class OnOffTrait(_Trait):
|
|||
return {'on': self.state.state != cover.STATE_CLOSED}
|
||||
return {'on': self.state.state != STATE_OFF}
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute an OnOff command."""
|
||||
domain = self.state.domain
|
||||
|
||||
|
@ -220,7 +219,7 @@ class OnOffTrait(_Trait):
|
|||
service_domain = domain
|
||||
service = SERVICE_TURN_ON if params['on'] else SERVICE_TURN_OFF
|
||||
|
||||
await hass.services.async_call(service_domain, service, {
|
||||
await self.hass.services.async_call(service_domain, service, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id
|
||||
}, blocking=True)
|
||||
|
||||
|
@ -268,14 +267,14 @@ class ColorSpectrumTrait(_Trait):
|
|||
return (command in self.commands and
|
||||
'spectrumRGB' in params.get('color', {}))
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute a color spectrum command."""
|
||||
# Convert integer to hex format and left pad with 0's till length 6
|
||||
hex_value = "{0:06x}".format(params['color']['spectrumRGB'])
|
||||
color = color_util.color_RGB_to_hs(
|
||||
*color_util.rgb_hex_to_rgb_list(hex_value))
|
||||
|
||||
await hass.services.async_call(light.DOMAIN, SERVICE_TURN_ON, {
|
||||
await self.hass.services.async_call(light.DOMAIN, SERVICE_TURN_ON, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
light.ATTR_HS_COLOR: color
|
||||
}, blocking=True)
|
||||
|
@ -331,7 +330,7 @@ class ColorTemperatureTrait(_Trait):
|
|||
return (command in self.commands and
|
||||
'temperature' in params.get('color', {}))
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute a color temperature command."""
|
||||
temp = color_util.color_temperature_kelvin_to_mired(
|
||||
params['color']['temperature'])
|
||||
|
@ -344,7 +343,7 @@ class ColorTemperatureTrait(_Trait):
|
|||
"Temperature should be between {} and {}".format(min_temp,
|
||||
max_temp))
|
||||
|
||||
await hass.services.async_call(light.DOMAIN, SERVICE_TURN_ON, {
|
||||
await self.hass.services.async_call(light.DOMAIN, SERVICE_TURN_ON, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
light.ATTR_COLOR_TEMP: temp,
|
||||
}, blocking=True)
|
||||
|
@ -376,12 +375,13 @@ class SceneTrait(_Trait):
|
|||
"""Return scene query attributes."""
|
||||
return {}
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute a scene command."""
|
||||
# Don't block for scripts as they can be slow.
|
||||
await hass.services.async_call(self.state.domain, SERVICE_TURN_ON, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id
|
||||
}, blocking=self.state.domain != script.DOMAIN)
|
||||
await self.hass.services.async_call(
|
||||
self.state.domain, SERVICE_TURN_ON, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id
|
||||
}, blocking=self.state.domain != script.DOMAIN)
|
||||
|
||||
|
||||
@register_trait
|
||||
|
@ -425,7 +425,8 @@ class TemperatureSettingTrait(_Trait):
|
|||
|
||||
return {
|
||||
'availableThermostatModes': ','.join(modes),
|
||||
'thermostatTemperatureUnit': _google_temp_unit(self.state),
|
||||
'thermostatTemperatureUnit': _google_temp_unit(
|
||||
self.hass.config.units.temperature_unit)
|
||||
}
|
||||
|
||||
def query_attributes(self):
|
||||
|
@ -437,7 +438,7 @@ class TemperatureSettingTrait(_Trait):
|
|||
if operation is not None and operation in self.hass_to_google:
|
||||
response['thermostatMode'] = self.hass_to_google[operation]
|
||||
|
||||
unit = self.state.attributes[ATTR_UNIT_OF_MEASUREMENT]
|
||||
unit = self.hass.config.units.temperature_unit
|
||||
|
||||
current_temp = attrs.get(climate.ATTR_CURRENT_TEMPERATURE)
|
||||
if current_temp is not None:
|
||||
|
@ -465,10 +466,10 @@ class TemperatureSettingTrait(_Trait):
|
|||
|
||||
return response
|
||||
|
||||
async def execute(self, hass, command, params):
|
||||
async def execute(self, command, params):
|
||||
"""Execute a temperature point or mode command."""
|
||||
# All sent in temperatures are always in Celsius
|
||||
unit = self.state.attributes[ATTR_UNIT_OF_MEASUREMENT]
|
||||
unit = self.hass.config.units.temperature_unit
|
||||
min_temp = self.state.attributes[climate.ATTR_MIN_TEMP]
|
||||
max_temp = self.state.attributes[climate.ATTR_MAX_TEMP]
|
||||
|
||||
|
@ -482,7 +483,7 @@ class TemperatureSettingTrait(_Trait):
|
|||
"Temperature should be between {} and {}".format(min_temp,
|
||||
max_temp))
|
||||
|
||||
await hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
climate.DOMAIN, climate.SERVICE_SET_TEMPERATURE, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
climate.ATTR_TEMPERATURE: temp
|
||||
|
@ -508,7 +509,7 @@ class TemperatureSettingTrait(_Trait):
|
|||
"Lower bound for temperature range should be between "
|
||||
"{} and {}".format(min_temp, max_temp))
|
||||
|
||||
await hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
climate.DOMAIN, climate.SERVICE_SET_TEMPERATURE, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
climate.ATTR_TARGET_TEMP_HIGH: temp_high,
|
||||
|
@ -516,7 +517,7 @@ class TemperatureSettingTrait(_Trait):
|
|||
}, blocking=True)
|
||||
|
||||
elif command == COMMAND_THERMOSTAT_SET_MODE:
|
||||
await hass.services.async_call(
|
||||
await self.hass.services.async_call(
|
||||
climate.DOMAIN, climate.SERVICE_SET_OPERATION_MODE, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
climate.ATTR_OPERATION_MODE:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue