def test_add_method_should_append_notification_to_list_and_return_uuid( self, uuid4): result = Notifications.add('warning', 'No title', 'No text') self.assertEqual(result, 'in-test') expected_list = self.notifications[:] expected_list.append({ 'uuid': 'in-test', 'type': 'warning', 'title': 'No title', 'text': 'No text' }) self.assertEqual(Notifications.list(), expected_list)
def run(store, conn, cursor): url = 'http://www.energo-pro.ge/category/electricity-suspensions/' # Reset stored values if needed / restore blinking after restart if is_notification_shown(): store.delete(BLINKING_STORE_KEY) if gateway_led.is_blocked(): gateway_led.unblock() else: Thread(target=blink, args=(store, )).start() store.set(BLINKING_STORE_KEY, 1) while True: try: page = pq(url) except: # it will be a connection error or something like that anyway sleep(10) continue if AREA in str(page).lower(): for el in page('.susp-table .bwm-post.post').items(): region, location, customers, date, time_from, time_to = [ el('div').eq(i).text() for i in range(6) ] if AREA in region.lower() or AREA in location.lower(): if not add_to_parsed(store, el.text()): continue text = '<b>Location</b>: {}, {}<br/><b>When</b>: {}, from {} to {}<br/>'.format( region, location, date, time_from, time_to) if not is_notification_exists(text): Notifications.add('error', NOTIFICATION_TITLE, text) gateway_speaker.play(10008, 5) if not store.get(BLINKING_STORE_KEY): Thread(target=blink, args=(store, )).start() store.set(BLINKING_STORE_KEY, 1) sleep()
def on_message(self, message): from plugins import gateway_led, yeelight message = json.loads(message) device = message.get('device') command = message.get('command') if device == gateway_led.DEVICE: if command == 'color': value = message['value'].replace('#', '') gateway_led.set_color(value) elif command == 'brightness': value = message['value'] gateway_led.set_brightness(value) elif command == 'rgb': value = message['value'].replace('#', '') gateway_led.set_rgb(value) elif command == 'toggle': gateway_led.toggle() self.write_message({'device': gateway_led.DEVICE, 'return': 'ok'}) elif device == yeelight.DEVICE: return_result = 'ok' if command == 'set_power': yeelight.set_power(message['power'], device_id=message['id']) elif command == 'set_name': yeelight.set_name(message['name'], device_id=message['id']) elif command == 'set_bright': yeelight.set_bright(message['bright'], device_id=message['id']) else: return_result = 'error' data = { 'device': yeelight.DEVICE, 'return': return_result } self.write_message(data) if message.get('kind') == 'notification': if message.get('command') == 'read': Notifications.remove(message.get('uuid'))
def test_remove_method_when_no_such_notification_in_list_should_do_nothing( self): Notifications.remove('0de8916f-7595-41b0-8e46-c9eea962b0b8') self.assertEqual(Notifications.list(), self.notifications)
def test_remove_method_should_remove_notification_from_list(self): Notifications.remove('second') expected_list = self.notifications[:1] self.assertEqual(Notifications.list(), expected_list)
def test_list_method_should_return_proper_notifications_list(self): self.assertEqual(Notifications.list(), self.notifications)
def is_notification_shown(): for item in Notifications.list(): if item['title'] == NOTIFICATION_TITLE: return False return True
def is_notification_exists(text): for item in Notifications.list(): if item['text'] == text: return True return False
def get(self): from plugins import gateway_led, yeelight cursor = get_cursor() cursor.execute('SELECT DISTINCT ON (sid) sid, temperature, humidity FROM ht ORDER BY sid, dt DESC') sensors_current = [] sensors_data = {} for sid, temperature, humidity in cursor.fetchall(): sensors_current.append({ 'sid': sid, 'name': config.SENSORS.get(sid, {}).get('name', sid), 'temperature': format_value(temperature, split=True), #'{:0.2f}'.format(temperature/100.0), 'humidity': format_value(humidity, split=True) #'{:0.2f}'.format(humidity/100.0) }) sensors_data[sid] = { 'labels': [], 'datasets': [ { 'label': 'Temperature', 'data': [], 'borderColor': '#bf3d3d' }, { 'label': 'Humidity', 'data': [], 'borderColor': '#b7bce5' } ] } cursor.execute('SELECT sid, temperature, humidity, dt FROM ht WHERE sid = %s ORDER BY dt DESC LIMIT 25', (sid,)) for sid, temperature, humidity, dt in reversed(cursor.fetchall()): sensors_data[sid]['labels'].append(dt.isoformat()) sensors_data[sid]['datasets'][0]['data'].append(format_value(temperature)) sensors_data[sid]['datasets'][1]['data'].append(format_value(humidity)) bg_images = map(lambda x: '/static/img/bg/%s' % x, os.listdir(os.path.join(os.path.dirname(__file__), "static", "img", "bg"))) bg_images = list(filter(lambda x: x.endswith('.jpg'), bg_images)) brightness, color, status = gateway_led.get_status() brightness = int(brightness, 16) / 100 magnets = [] for sid, sensor in config.SENSORS.items(): if sensor.get('device') == 'magnet': magnet_status = get_store().get('magnet_{}'.format(sid)) magnets.append({ 'sid': sid, 'name': sensor.get('name'), 'status': magnet_status.decode() if magnet_status else 'open', }) self.render( "templates/index.html", sensors=config.SENSORS, sensors_current=sensors_current, sensors_data=sensors_data, magnets=magnets, bg_images=bg_images, gateway_led={ 'brightness': hex(int(brightness*100))[2:], 'color': color, 'status': status }, bulbs=yeelight.get_devices(), notifications=Notifications.list() )