diff --git a/home-assistant/README.md b/home-assistant/README.md new file mode 100644 index 0000000..5a7324a --- /dev/null +++ b/home-assistant/README.md @@ -0,0 +1,3 @@ +# Home Assistant integration + +This is an experimental [home assistant](https://www.home-assistant.io/) integration for meteo sensor. Place the `meteo` folder to your `CONFIG/custom_components` folder on your home-assistant intallation (See [Creating your first integration](https://developers.home-assistant.io/docs/creating_component_index) page for more info) diff --git a/home-assistant/meteo/__init__.py b/home-assistant/meteo/__init__.py new file mode 100644 index 0000000..adb8bfb --- /dev/null +++ b/home-assistant/meteo/__init__.py @@ -0,0 +1,77 @@ +""" +meteo MQTT component + +Listens for meteo messages on MQTT and updates the state accordingly. + +Offets a service 'meteo' + +Also offers a service 'set_state' that will publish a message on the topic that +will be passed via MQTT to our message received listener. Call the service with +example payload {"new_state": "some new state"}. + +Configuration: + +To use the meteo component you will need to add the following to your +configuration.yaml file. + +meteo: + topic: "meteo" +""" +from homeassistant.components import mqtt + +import voluptuous as vol +import json +import logging + +_LOGGER = logging.getLogger(__name__) + +# The domain of your component. Should be equal to the name of your component. +DOMAIN = "meteo" + +CONF_TOPIC = 'topic' +DEFAULT_TOPIC = 'meteo/#' + +## Schema to validate the configured MQTT topic +#CONFIG_SCHEMA = vol.Schema({ +# vol.Optional(CONF_TOPIC, default=DEFAULT_TOPIC): mqtt.valid_subscribe_topic +#}) + + +def friendly_name(name) : + if name == "t" : return "Temperature" + elif name == "hum" : return "Humidity" + elif name == "p" : return "Pressure" + else : return name + +def setup(hass, config): + topic = config[DOMAIN][CONF_TOPIC] + hass.states.set("meteo.present", "yes") + + # Listen to a message on MQTT. + def received(topic, payload, qos): + # Parse json + try : + j = json.loads(payload) + station_id = int(j['id']) + station_name = j['name'] + hass.states.set("meteo.%d"%station_id, station_name) + base = "sensor.meteo_%d" % station_id + hass.states.set(base + "_name", station_name) + attr = {} + for x in j : + if x in ["id","name"] : continue + try : + attr = {} + attr['friendly_name'] = "%s %s" % (station_name, friendly_name(x)) + hass.states.set(base + "_" + x, float(j[x]),attr) + except ValueError : + continue + except Exception as e: + _LOGGER.info("meteo invalid json received: %s (%s)" % (payload, e)) + # Ignore for now + return + + hass.components.mqtt.subscribe(topic, received) + + # Return boolean to indicate that initialization was successfully. + return True diff --git a/home-assistant/meteo/manifest.json b/home-assistant/meteo/manifest.json new file mode 100644 index 0000000..e3d5bea --- /dev/null +++ b/home-assistant/meteo/manifest.json @@ -0,0 +1,9 @@ +{ + "domain": "meteo", + "name": "meteo", + "documentation": "github.com/grisu48/meteo", + "dependencies": ["mqtt"], + "codeowners": [], + "requirements": [] +} +