Skip to content

Commit

Permalink
Move labels to env_canada
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldavie committed Jul 2, 2019
1 parent 9c5b59b commit c943bdf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 94 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/environment_canada/manifest.json
Expand Up @@ -3,7 +3,7 @@
"name": "Environment Canada",
"documentation": "https://www.home-assistant.io/components/environment_canada",
"requirements": [
"env_canada==0.0.14"
"env_canada==0.0.16"
],
"dependencies": [],
"codeowners": [
Expand Down
130 changes: 53 additions & 77 deletions homeassistant/components/environment_canada/sensor.py
Expand Up @@ -32,64 +32,31 @@

MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(minutes=10)

SENSOR_TYPES = {
'temperature': {'english': 'Temperature',
'french': 'Température',
'unit': TEMP_CELSIUS},
'dewpoint': {'english': 'Dew Point',
'french': 'Point de rosée',
'unit': TEMP_CELSIUS},
'wind_chill': {'english': 'Wind Chill',
'french': 'Refroidissement éolien',
'unit': TEMP_CELSIUS},
'humidex': {'english': 'Humidex',
'french': 'Humidex',
'unit': TEMP_CELSIUS},
'pressure': {'english': 'Pressure',
'french': 'Pression',
'unit': 'kPa'},
'tendency': {'english': 'Tendency',
'french': 'Tendance'},
'humidity': {'english': 'Humidity',
'french': 'Humidité',
'unit': '%'},
'visibility': {'english': 'Visibility',
'french': 'Visibilité',
'unit': 'km'},
'condition': {'english': 'Condition',
'french': 'Condition'},
'wind_speed': {'english': 'Wind Speed',
'french': 'Vitesse de vent',
'unit': 'km/h'},
'wind_gust': {'english': 'Wind Gust',
'french': 'Rafale de vent',
'unit': 'km/h'},
'wind_dir': {'english': 'Wind Direction',
'french': 'Direction de vent'},
'high_temp': {'english': 'High Temperature',
'french': 'Haute température',
'unit': TEMP_CELSIUS},
'low_temp': {'english': 'Low Temperature',
'french': 'Basse température',
'unit': TEMP_CELSIUS},
'pop': {'english': 'Chance of Precip.',
'french': 'Probabilité d\'averses',
'unit': '%'},
'forecast_period': {'english': 'Forecast Period',
'french': 'Période de prévision'},
'text_summary': {'english': 'Text Summary',
'french': 'Résumé textuel'},
'warnings': {'english': 'Warnings',
'french': 'Alertes'},
'watches': {'english': 'Watches',
'french': 'Veilles'},
'advisories': {'english': 'Advisories',
'french': 'Avis'},
'statements': {'english': 'Statements',
'french': 'Bulletins'},
'endings': {'english': 'Ended',
'french': 'Terminé'}
}
SENSOR_TYPES = [
'temperature',
'dewpoint',
'wind_chill',
'humidex',
'pressure',
'tendency',
'humidity',
'visibility',
'condition',
'wind_speed',
'wind_gust',
'wind_dir',
'wind_bearing',
'forecast_period',
'text_summary',
'high_temp',
'low_temp',
'pop',
'warnings',
'watches',
'advisories',
'statements',
'endings'
]


def validate_station(station):
Expand All @@ -104,12 +71,12 @@ def validate_station(station):
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
vol.Required(CONF_LANGUAGE, default='english'):
vol.In(['english', 'french']),
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_STATION): validate_station,
vol.Inclusive(CONF_LATITUDE, 'latlon'): cv.latitude,
vol.Inclusive(CONF_LONGITUDE, 'latlon'): cv.longitude,
vol.Optional(CONF_LANGUAGE, default='english'):
vol.In(['english', 'french'])
})


Expand All @@ -131,31 +98,31 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

add_devices([ECSensor(sensor_type,
ec_data,
config.get(CONF_NAME),
config.get(CONF_LANGUAGE))
config.get(CONF_NAME))
for sensor_type in config[CONF_MONITORED_CONDITIONS]],
True)


class ECSensor(Entity):
"""Implementation of an Environment Canada sensor."""

def __init__(self, sensor_type, ec_data, platform_name, language):
def __init__(self, sensor_type, ec_data, platform_name):
"""Initialize the sensor."""
self.sensor_type = sensor_type
self.ec_data = ec_data
self.platform_name = platform_name
self._state = None
self._attr = None
self.language = language
self._data = None
self._name = None
self._unit = None

@property
def name(self):
"""Return the name of the sensor."""
name = SENSOR_TYPES[self.sensor_type][self.language]
if self.platform_name is None:
return name
return ' '.join([self.platform_name, name])
return self._name
return ' '.join([self.platform_name, self._name])

@property
def state(self):
Expand All @@ -170,30 +137,39 @@ def device_state_attributes(self):
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return SENSOR_TYPES[self.sensor_type].get('unit')
return self._unit

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Update current conditions."""
self.ec_data.update()
self.ec_data.conditions.update(self.ec_data.alerts)

conditions = self.ec_data.conditions
sensor_data = conditions.get(self.sensor_type)

self._attr = {}
self._name = sensor_data.get('label')
value = sensor_data.get('value')

sensor_data = self.ec_data.conditions.get(self.sensor_type)
if isinstance(sensor_data, list):
if isinstance(value, list):
self._state = ' | '.join([str(s.get('title'))
for s in sensor_data])
for s in value])
self._attr.update({
ATTR_DETAIL: ' | '.join([str(s.get('detail'))
for s in sensor_data]),
for s in value]),
ATTR_TIME: ' | '.join([str(s.get('date'))
for s in sensor_data])
for s in value])
})
else:
self._state = sensor_data
self._state = value

if sensor_data.get('unit') == 'C':
self._unit = TEMP_CELSIUS
else:
self._unit = sensor_data.get('unit')

timestamp = self.ec_data.conditions.get('timestamp')
timestamp = self.ec_data.conditions.get('timestamp').get('value')
if timestamp:
updated_utc = datetime.datetime.strptime(timestamp, '%Y%m%d%H%M%S')
updated_local = dt.as_local(updated_utc).isoformat()
Expand All @@ -205,7 +181,7 @@ def update(self):
self._attr.update({
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_UPDATED: updated_local,
ATTR_LOCATION: self.ec_data.conditions.get('location'),
ATTR_STATION: self.ec_data.conditions.get('station'),
ATTR_LOCATION: conditions.get('location').get('value'),
ATTR_STATION: conditions.get('station').get('value'),
ATTR_HIDDEN: hidden
})
30 changes: 15 additions & 15 deletions homeassistant/components/environment_canada/weather.py
Expand Up @@ -96,13 +96,13 @@ def name(self):
"""Return the name of the weather entity."""
if self.platform_name:
return self.platform_name
return self.ec_data.conditions['location']
return self.ec_data.conditions.get('location').get('value')

@property
def temperature(self):
"""Return the temperature."""
if self.ec_data.conditions.get('temperature'):
return float(self.ec_data.conditions['temperature'])
if self.ec_data.conditions.get('temperature').get('value'):
return float(self.ec_data.conditions['temperature']['value'])
return None

@property
Expand All @@ -113,45 +113,45 @@ def temperature_unit(self):
@property
def humidity(self):
"""Return the humidity."""
if self.ec_data.conditions.get('humidity'):
return float(self.ec_data.conditions['humidity'])
if self.ec_data.conditions.get('humidity').get('value'):
return float(self.ec_data.conditions['humidity']['value'])
return None

@property
def wind_speed(self):
"""Return the wind speed."""
if self.ec_data.conditions.get('wind_speed'):
return float(self.ec_data.conditions['wind_speed'])
if self.ec_data.conditions.get('wind_speed').get('value'):
return float(self.ec_data.conditions['wind_speed']['value'])
return None

@property
def wind_bearing(self):
"""Return the wind bearing."""
if self.ec_data.conditions.get('wind_bearing'):
return float(self.ec_data.conditions['wind_bearing'])
if self.ec_data.conditions.get('wind_bearing').get('value'):
return float(self.ec_data.conditions['wind_bearing']['value'])
return None

@property
def pressure(self):
"""Return the pressure."""
if self.ec_data.conditions.get('pressure'):
return 10 * float(self.ec_data.conditions['pressure'])
if self.ec_data.conditions.get('pressure').get('value'):
return 10 * float(self.ec_data.conditions['pressure']['value'])
return None

@property
def visibility(self):
"""Return the visibility."""
if self.ec_data.conditions.get('visibility'):
return float(self.ec_data.conditions['visibility'])
if self.ec_data.conditions.get('visibility').get('value'):
return float(self.ec_data.conditions['visibility']['value'])
return None

@property
def condition(self):
"""Return the weather condition."""
icon_code = self.ec_data.conditions.get('icon_code')
icon_code = self.ec_data.conditions.get('icon_code').get('value')
if icon_code:
return icon_code_to_condition(int(icon_code))
condition = self.ec_data.conditions.get('condition')
condition = self.ec_data.conditions.get('condition').get('value')
if condition:
return condition
return 'Condition not observed'
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Expand Up @@ -415,7 +415,7 @@ enocean==0.50
enturclient==0.2.0

# homeassistant.components.environment_canada
env_canada==0.0.14
env_canada==0.0.16

# homeassistant.components.envirophat
# envirophat==0.0.6
Expand Down

0 comments on commit c943bdf

Please sign in to comment.