This commit is contained in:
parent
36524e9d3f
commit
f9f53fd278
3 changed files with 0 additions and 215 deletions
|
@ -39,8 +39,6 @@ async def async_setup(hass, config):
|
|||
await api.async_connect()
|
||||
hass.data[DATA_MELISSA] = api
|
||||
|
||||
hass.async_create_task(
|
||||
async_load_platform(hass, 'sensor', DOMAIN, {}, config))
|
||||
hass.async_create_task(
|
||||
async_load_platform(hass, 'climate', DOMAIN, {}, config))
|
||||
return True
|
||||
|
|
|
@ -1,101 +0,0 @@
|
|||
"""
|
||||
Support for Melissa climate Sensors.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.melissa/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.melissa import DATA_MELISSA
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
DEPENDENCIES = ['melissa']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the melissa sensor platform."""
|
||||
sensors = []
|
||||
api = hass.data[DATA_MELISSA]
|
||||
|
||||
devices = (await api.async_fetch_devices()).values()
|
||||
|
||||
for device in devices:
|
||||
if device['type'] == 'melissa':
|
||||
sensors.append(MelissaTemperatureSensor(device, api))
|
||||
sensors.append(MelissaHumiditySensor(device, api))
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class MelissaSensor(Entity):
|
||||
"""Representation of a Melissa Sensor."""
|
||||
|
||||
_type = 'generic'
|
||||
|
||||
def __init__(self, device, api):
|
||||
"""Initialize the sensor."""
|
||||
self._api = api
|
||||
self._state = None
|
||||
self._name = '{0} {1}'.format(
|
||||
device['name'],
|
||||
self._type
|
||||
)
|
||||
self._serial = device['serial_number']
|
||||
self._data = device['controller_log']
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
async def async_update(self):
|
||||
"""Fetch status from melissa."""
|
||||
self._data = await self._api.async_status(cached=True)
|
||||
|
||||
|
||||
class MelissaTemperatureSensor(MelissaSensor):
|
||||
"""Representation of a Melissa temperature Sensor."""
|
||||
|
||||
_type = 'temperature'
|
||||
_unit = TEMP_CELSIUS
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
return self._unit
|
||||
|
||||
async def async_update(self):
|
||||
"""Fetch new state data for the sensor."""
|
||||
await super().async_update()
|
||||
try:
|
||||
self._state = self._data[self._serial]['temp']
|
||||
except KeyError:
|
||||
_LOGGER.warning("Unable to get temperature for %s", self.entity_id)
|
||||
|
||||
|
||||
class MelissaHumiditySensor(MelissaSensor):
|
||||
"""Representation of a Melissa humidity Sensor."""
|
||||
|
||||
_type = 'humidity'
|
||||
_unit = '%'
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
return self._unit
|
||||
|
||||
async def async_update(self):
|
||||
"""Fetch new state data for the sensor."""
|
||||
await super().async_update()
|
||||
try:
|
||||
self._state = self._data[self._serial]['humidity']
|
||||
except KeyError:
|
||||
_LOGGER.warning("Unable to get humidity for %s", self.entity_id)
|
|
@ -1,112 +0,0 @@
|
|||
"""Test for Melissa climate component."""
|
||||
import json
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.components.sensor.melissa import MelissaTemperatureSensor, \
|
||||
MelissaHumiditySensor
|
||||
|
||||
from tests.common import load_fixture, mock_coro_func
|
||||
|
||||
from homeassistant.components.melissa import DATA_MELISSA
|
||||
from homeassistant.components.sensor import melissa
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
|
||||
|
||||
_SERIAL = "12345678"
|
||||
|
||||
|
||||
def melissa_mock():
|
||||
"""Use this to mock the melissa api."""
|
||||
api = Mock()
|
||||
api.async_fetch_devices = mock_coro_func(
|
||||
return_value=json.loads(load_fixture('melissa_fetch_devices.json')))
|
||||
api.async_status = mock_coro_func(return_value=json.loads(load_fixture(
|
||||
'melissa_status.json'
|
||||
)))
|
||||
|
||||
api.TEMP = 'temp'
|
||||
api.HUMIDITY = 'humidity'
|
||||
return api
|
||||
|
||||
|
||||
async def test_setup_platform(hass):
|
||||
"""Test setup_platform."""
|
||||
with patch('homeassistant.components.melissa'):
|
||||
hass.data[DATA_MELISSA] = melissa_mock()
|
||||
|
||||
config = {}
|
||||
async_add_entities = mock_coro_func()
|
||||
discovery_info = {}
|
||||
|
||||
await melissa.async_setup_platform(
|
||||
hass, config, async_add_entities, discovery_info)
|
||||
|
||||
|
||||
async def test_name(hass):
|
||||
"""Test name property."""
|
||||
with patch('homeassistant.components.melissa'):
|
||||
mocked_melissa = melissa_mock()
|
||||
device = (await mocked_melissa.async_fetch_devices())[_SERIAL]
|
||||
temp = MelissaTemperatureSensor(device, mocked_melissa)
|
||||
hum = MelissaHumiditySensor(device, mocked_melissa)
|
||||
|
||||
assert temp.name == '{0} {1}'.format(
|
||||
device['name'],
|
||||
temp._type
|
||||
)
|
||||
assert hum.name == '{0} {1}'.format(
|
||||
device['name'],
|
||||
hum._type
|
||||
)
|
||||
|
||||
|
||||
async def test_state(hass):
|
||||
"""Test state property."""
|
||||
with patch('homeassistant.components.melissa'):
|
||||
mocked_melissa = melissa_mock()
|
||||
device = (await mocked_melissa.async_fetch_devices())[_SERIAL]
|
||||
status = (await mocked_melissa.async_status())[_SERIAL]
|
||||
temp = MelissaTemperatureSensor(device, mocked_melissa)
|
||||
hum = MelissaHumiditySensor(device, mocked_melissa)
|
||||
await temp.async_update()
|
||||
assert temp.state == status[mocked_melissa.TEMP]
|
||||
await hum.async_update()
|
||||
assert hum.state == status[mocked_melissa.HUMIDITY]
|
||||
|
||||
|
||||
async def test_unit_of_measurement(hass):
|
||||
"""Test unit of measurement property."""
|
||||
with patch('homeassistant.components.melissa'):
|
||||
mocked_melissa = melissa_mock()
|
||||
device = (await mocked_melissa.async_fetch_devices())[_SERIAL]
|
||||
temp = MelissaTemperatureSensor(device, mocked_melissa)
|
||||
hum = MelissaHumiditySensor(device, mocked_melissa)
|
||||
assert temp.unit_of_measurement == TEMP_CELSIUS
|
||||
assert hum.unit_of_measurement == '%'
|
||||
|
||||
|
||||
async def test_update(hass):
|
||||
"""Test for update."""
|
||||
with patch('homeassistant.components.melissa'):
|
||||
mocked_melissa = melissa_mock()
|
||||
device = (await mocked_melissa.async_fetch_devices())[_SERIAL]
|
||||
temp = MelissaTemperatureSensor(device, mocked_melissa)
|
||||
hum = MelissaHumiditySensor(device, mocked_melissa)
|
||||
await temp.async_update()
|
||||
assert temp.state == 27.4
|
||||
await hum.async_update()
|
||||
assert hum.state == 18.7
|
||||
|
||||
|
||||
async def test_update_keyerror(hass):
|
||||
"""Test for faulty update."""
|
||||
with patch('homeassistant.components.melissa'):
|
||||
mocked_melissa = melissa_mock()
|
||||
device = (await mocked_melissa.async_fetch_devices())[_SERIAL]
|
||||
temp = MelissaTemperatureSensor(device, mocked_melissa)
|
||||
hum = MelissaHumiditySensor(device, mocked_melissa)
|
||||
mocked_melissa.async_status = mock_coro_func(return_value={})
|
||||
await temp.async_update()
|
||||
assert temp.state is None
|
||||
await hum.async_update()
|
||||
assert hum.state is None
|
Loading…
Add table
Add a link
Reference in a new issue